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
Enabling Eslint on typescript files
提问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
--ext
allows 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 typescript
plugin and typescript-eslint-parser
and 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 .vue
and my .ts
files 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"]
}