在打字稿文件上启用 Eslint

Enabling Eslint on typescript files

在 webstorm eslint 设置中,有一个 "extra eslint options" 字段。 在此,我添加了:

--ext .ts

来自 eslint documentation,它应该允许 eslint 处理自定义文件扩展名,在本例中为 .ts 文件。 这没有任何作用。我的语法错了吗?无论如何要在 .ts 文件上启用 Eslint,可能来自 .eslintrc 文件?

--ext 允许使用自定义 javascript 扩展,您不能通过向其传递不同的文件扩展名来强制 ESLint 为 JavaScript 以外的语言工作。

您可以尝试使用 typescript-eslint-parser 为 Typescript 启用 ESLint - 它允许从可以传递给 ESLint 的 typescript 代码构建语法树。

但我建议使用 Typescript linters 来检查 TypeScript 代码。例如,您可以尝试 TSLint

更新:从2017.1.3开始,WebStorm支持ESLint + typescript-eslint-parser;你只需要安装 typescript 插件和 typescript-eslint-parser 并相应地修改你的 ESLint 配置:

"parser": "typescript-eslint-parser",
"plugins": ["typescript"]

尝试将此行添加到您的 .eslintrc.json

"plugins": ["typescript"]

在我的例子中,我使用以下配置对 .vue.ts 文件进行了 eslint 格式化:

{
  "parser": "vue-eslint-parser",
  "parserOptions": {
     "parser": "typescript-eslint-parser"
   },
  "extends": [
     "eslint:recommended",
     "plugin:vue/recommended",
     "typescript"
  ],
  "plugins": ["typescript"]
}

2020 年答案:

我的问题是 ESLint errors/warnings 没有出现在 WebStorm 的“问题”选项卡中(这也适用于 Rider)对于 .ts 文件,即使 VS Code 工作正常(显示 warnings/errors) 对于同一个文件。

解决方案:

  1. 按 CTRL+SHIFT+A

  1. 搜索“注册表...”

  1. 开始键入 eslint.additional.file.extensions 以查找该注册表项

  1. eslint.additional.file.extensions 的值设置为 js,ts(如上面的屏幕截图所示)

您可能应该重新启动编辑器以确保万无一失。这对我有用(errors/warnings 开始出现在问题部分,就像在 VS 代码中一样)并且我正在使用 @typescript-eslint/[somethinghere] 规则等