typescript 在打字稿文件上启用 Eslint

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36910592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:26:46  来源:igfitidea点击:

Enabling Eslint on typescript files

typescriptwebstormeslint

提问by Robert Brax

On webstorm eslint setting, there is an "extra eslint options" field. In this, I added:

在 webstorm eslint 设置中,有一个“额外的 eslint 选项”字段。在此,我补充说:

--ext .ts

from the eslint documentationwhich is supposed to allow eslint to work on custom file extension, in this case .ts files. This does nothing. Is my syntax wrong ? Anyway to enable Eslint on .ts files, maybe from the .eslintrc file ?

来自 eslint文档,它应该允许 eslint 处理自定义文件扩展名,在这种情况下是 .ts 文件。这没有任何作用。我的语法错了吗?无论如何要在 .ts 文件上启用 Eslint,也许来自 .eslintrc 文件?

回答by lena

--extallows using custom javascript extensions, you can't force ESLint to work for languages other than JavaScript by passing a different file extension to it.

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

You can try using typescript-eslint-parserto enable ESLint for Typescript - it allows building a syntax tree from typescript code that can be passed to ESLint for linting.

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

But I'd suggest using Typescript linters for inspecting TypeScript code. You can try TSLint, for example.

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

Update: since 2017.1.3, WebStorm supports ESLint + typescript-eslint-parser; you just need to install both typescriptplugin and typescript-eslint-parserand modify your ESLint config accordingly:

更新:自 2017.1.3 起,WebStorm 支持 ESLint + typescript-eslint-parser;您只需要安装typescript插件typescript-eslint-parser并相应地修改您的 ESLint 配置:

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

回答by Marie

Try adding this line into your .eslintrc.json

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

"plugins": ["typescript"]

In my case, I made eslint format my .vueand my .tsfiles by using this config :

就我而言,我使用以下配置使 eslint 格式化我.vue和我的.ts文件:

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