ESLint 要求分号并同时将其删除

MrL*_*oon 16 typescript eslint

我正在配置 ESLint(因为 TSLint 即将被弃用)来处理我的 .ts 文件。我有这个很小的文件:

export default interface Departure {
  line: string;
  direction: string;
  time: Date;
};
Run Code Online (Sandbox Code Playgroud)

在最后一次,分号所在的位置,我的 VSCode 中的 ESLint 发出两个错误信号:一个是关于缺少分号eslint(semi),另一个是关于不必要的分号eslint(no-extra-semi)

eslint 分号错误

以下是我的.eslintrc.js

module.exports = {
    "env": {
        "browser": true,
        "commonjs": true,
        "es6": true,
        "node": true
    },
    "extends": [
        "plugin:@typescript-eslint/recommended",
        "airbnb"
    ],
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaFeatures": {
            "jsx": true
        },
        "ecmaVersion": 2018
    },
    "plugins": [
        "react",
        "@typescript-eslint"
    ],
    "settings": {
        "import/resolver": {
            "node": {
                "extensions": [".js", ".jsx", ".ts", ".tsx"]
            }
        }
    },
    "rules": {
    }
};
Run Code Online (Sandbox Code Playgroud)

我怎样才能摆脱这种奇怪的情况?应该没有任何错误。

ole*_*huk 23

这里的问题是同时应用了 2 个规则:
1.export default需要有分号
2.interface需要去掉分号

此处的解决方法是将接口声明与导出语句分开,如下所示:

interface Departure {
  line: string;
  direction: string;
  time: Date;
}

export default Departure;
Run Code Online (Sandbox Code Playgroud)

我自己没有想出答案。来源是这个提交的问题


小智 11

添加 Ricardo Emerson 的评论,以下.eslintrc设置不会 关闭分号错误:

{
  ...
  "extends": [
    ...
    "eslint:recommended"
  ],
  "rules": {
    ...
    "semi": ["error", "never"],
    "@typescript-eslint/semi": "off",
    "no-unexpected-multiline": "error"
  }
}

Run Code Online (Sandbox Code Playgroud)

no-unexpected-multiline根据文档,还建议在关闭分号时打开错误。


Ric*_*son 6

我使用以下规则解决了这个问题:

"rules": {
  "semi": "off",
  "@typescript-eslint/semi": ["error"],
}
Run Code Online (Sandbox Code Playgroud)

  • 这会关闭各处缺少分号的错误,这不太好。 (4认同)
  • @Marko Bjelac 尝试“@typescript-eslint/semi”:[“错误”,“总是”]。否则它不会检查每一行,而只是检查语法不明确的行。这是我在打字稿中使用的确切设置,它确实报告缺少西米冒号。@typescript-eslint/semi 取代 TypeScript 代码中的“semi”。 (2认同)