我在具有以下设置的所有Typescript项目中使用esLint:
"extends": ["airbnb", "prettier", 'plugin:vue/recommended'],
"plugins": ["prettier"],
"parserOptions": {
"parser": "@typescript-eslint/parser",
"ecmaVersion": 2018,
"sourceType": "module"
},
Run Code Online (Sandbox Code Playgroud)
+一堆自定义规则。我还为Typescript支持安装了以下依赖项:
"@typescript-eslint/eslint-plugin": "^1.7.0",
"@typescript-eslint/parser": "^1.7.0",
Run Code Online (Sandbox Code Playgroud)
但是,esLint最有用的规则之一https://eslint.org/docs/rules/no-unused-vars对于Typescript项目而言似乎配置很差。例如,当我导出一个枚举时,该规则警告我该枚举在声明它的文件中没有被使用:
export enum Foo {
Bar,
}
Run Code Online (Sandbox Code Playgroud)
类似地,当我导入要用作类型的接口或类时,“ no-unused-vars”将再次在实际导入的行中抱怨:
在脚
export interface Foo {
bar: string;
}
Run Code Online (Sandbox Code Playgroud)
在bar.ts中
import { Foo } from './Foo'
const bar: Foo = { bar: 'Hello' };
Run Code Online (Sandbox Code Playgroud)
有什么方法可以配置no-unused-vars规则来考虑这两种情况?我不喜欢禁用该规则,因为在这些案例之外,它是我整个规则集中最有用的规则之一。
我已经将规则降级为仅给出警告而不是错误,但是将我的所有文档都填充为警告仍然无法达到使用esLint的目的。
jai*_*rik 64
我认为使用会"plugin:@typescript-eslint/eslint-recommended"引入一堆不需要的规则。使用"@typescript-eslint/no-unused-vars"ESLint 规则可能会更好。
{
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
],
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error"]
}
}
Run Code Online (Sandbox Code Playgroud)
ghi*_*ing 48
我在最新的 TypeScript/ES-Lint 版本中遇到了很多错误的错误,我发现他们提出了一个实验规则来修复no-unused-vars被破坏的问题,并且通过实验规则@typescript-eslint/no-unused-vars-experimental它最终按我的预期工作。
在我这边改变之前,我在使用接口/类型时有多个错误错误,说这些变量没有被使用(当然它们永远不会被使用,因为它们不是变量而是接口/类型)......和如果您对代码本身感到好奇,这里是添加此实验规则的PR,这就是我发现规则的方式。
这是我更新.eslintrc文件的一个子集
{
"parser": "@typescript-eslint/parser",
"extends": [
"plugin:@typescript-eslint/recommended"
],
"rules": {
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-unused-vars-experimental": "error",
"no-unused-vars": "off"
}
}
Run Code Online (Sandbox Code Playgroud)
我现在终于恢复正常了:)
编辑(2021 年 1 月)
正如 Brad(项目的维护者)在下面的评论中提到的,这是(曾经)一个临时解决方案,现在已被弃用。根据他的评论(如下),我们现在可以直接@typescript-eslint/no-unused-vars用于相同的预期行为。感谢布拉德提供的信息。我还可以确认切换回@typescript-eslint/no-unused-vars现在对我有用(我更新了我的代码,现在一切都很好)。
自 2021 年 1 月以来更新的答案这不是要走的路,你应该避免它。
@typescript-eslint/no-unused-vars-experimental已弃用,并将在下一个专业中删除。更新到最新版本的工具,只需使用@typescript-eslint/no-unused-vars. 来源:我是该项目的维护者。
所以.eslintrc这是对我有用的文件的最新更新:)
{
"parser": "@typescript-eslint/parser",
"extends": [
"plugin:@typescript-eslint/recommended"
],
"rules": {
"@typescript-eslint/no-unused-vars": "error",
"no-unused-vars": "off"
}
}
Run Code Online (Sandbox Code Playgroud)
thi*_*ign 21
我的问题是使用装饰器并希望有一个具有适当名称的变量以便清楚起见,例如:
@OneToMany((type) => Employee)代替@OneToMany(() => Employee)
TypeScript 通常的解决方案是使用下划线作为前缀:
@OneToMany((_type) => Employee)
并且可以让 ESLint 接受相同的内容:
.eslintrc.js
module.exports = {
...
rules: {
'@typescript-eslint/no-unused-vars': ['warn', { 'argsIgnorePattern': '^_' }]
....
},
};
Run Code Online (Sandbox Code Playgroud)
And*_*w E 10
对于任何希望在使用YAML配置时让 no-unused-vars 在 TypeScript 中正常工作的人,例如.eslintrc.yaml,它看起来像这样:
rules:
"@typescript-eslint/no-unused-vars":
- warn
- argsIgnorePattern: "^_" # <-- NOTE!
varsIgnorePattern: "^_"
caughtErrorsIgnorePattern: "^_"
no-unused-vars: # disabled but see typescript-eslint/no-unused-vars
- off
...
Run Code Online (Sandbox Code Playgroud)
您已parser嵌套在parserOptions. 它应该是一个兄弟姐妹,像这样:
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
Run Code Online (Sandbox Code Playgroud)
至于no-unused-vars,恐怕这是一个持续存在的错误@typescript-eslint:
https://github.com/typescript-eslint/typescript-eslint/issues/363
小智 5
It's a bit buried in the documentation, but if you add some things to the 'extends' property, you can use both the rules recommended by ESLint like no-unused-vars, and have it actually work in Typescript. Like so:
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
Run Code Online (Sandbox Code Playgroud)
@typescript-eslint/recommended seems to be the thing that allows eslint:recommended to deal with Typescript constructs effectively. Not sure how it would affect your other extensions though.
| 归档时间: |
|
| 查看次数: |
1206 次 |
| 最近记录: |