关于ESLint:ESLint-为TypeScript配置” no-unused-vars”

ESLint - Configuring “no-unused-vars” for TypeScript

我在具有以下设置的所有TypeScript项目中使用ESLint:

1
2
3
4
5
6
7
 "extends": ["airbnb","prettier", 'plugin:vue/recommended'],
 "plugins": ["prettier"],
 "parserOptions": {
 "parser":"@typescript-eslint/parser",
 "ecmaVersion": 2018,
 "sourceType":"module"
  },
  • 一堆自定义规则。我还为TypeScript支持安装了以下依赖项:

    1
    2
     "@typescript-eslint/eslint-plugin":"^1.7.0",
     "@typescript-eslint/parser":"^1.7.0",

但是,ESLint最有用的规则之一https://eslint.org/docs/rules/no-unused-vars对于TypeScript项目似乎配置很差。例如,当我导出一个枚举时,该规则会警告我该枚举在声明它的文件中未被使用:

1
2
3
export enum Foo {
   Bar,
}

类似地,当我导入要用作类型的接口或类时,\\'no-unused-vars \\'将再次在实际导入的行上抱怨:

在Foo.ts

1
2
3
export interface Foo {
   bar: string;
}

在bar.ts中

1
2
import { Foo } from './Foo'
const bar: Foo = { bar: 'Hello' };

是否可以通过配置no-unused-vars规则来考虑这两种情况?我不喜欢禁用该规则,因为在这些情况下,它是我整个规则集中最有用的规则之一。

我已经将规则降级为仅给出警告而不是错误,但是让我的所有文档都充满警告仍然无法达到使用esLint的目的。

按照此处的建议使用// eslint-disable-line填充我的所有文档,这似乎也是一个糟糕的解决方案。


它有点埋在文档中,但是如果您在'extends'属性中添加一些内容,则可以使用ESLint推荐的规则(如no-unused-vars),并使其在Typescript中实际起作用。像这样:

1
2
3
4
5
"extends": [
       "eslint:recommended",
       "plugin:@typescript-eslint/eslint-recommended",
       "plugin:@typescript-eslint/recommended"
    ],

@ typescript-eslint / recommended似乎是允许eslint:recommended有效处理Typescript构造的东西。不确定它会如何影响您的其他扩展。


我认为"plugin:@typescript-eslint/recommended"的使用引入了许多不需要的规则。最好使用"@typescript-eslint/no-unused-vars" ESLint规则。

1
2
3
4
5
6
7
8
9
10
{
 "parser":"@typescript-eslint/parser",
 "extends": [
   "plugin:@typescript-eslint/recommended"
  ],
 "rules": {
   "no-unused-vars":"off",
   "@typescript-eslint/no-unused-vars": ["error"]
  }
}

参考-https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-vars.md


我在使用最新的TypeScript / ES-Lint版本时遇到了很多错误错误,我发现他们想出了一个修复no-unused-vars的实验规则,并修复了no-unused-vars的问题,最终它可以像我期待它。

在我这边进行更改之前,使用接口/类型时我遇到了多个错误错误,说这些变量是未使用的(当然它们将永远不会被使用,因为它们不是变量,而是接口/类型)。 ..以防您对代码本身感到好奇,这是PR添加了此实验性规则,这也是我找到规则的方式。

这是我更新的.eslintrc文件的子集

1
2
3
4
5
6
7
8
9
10
11
{
 "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"
  }
}

现在我终于恢复正常了:)

EDIT(2021年1月)

正如Brad(该项目的维护者)在下面的评论中提到的那样,这是(以前的)临时解决方案,现已弃用。根据他的评论(如下),我们现在可以直接将@typescript-eslint/no-unused-vars用于相同的预期行为。感谢Brad提供的信息。我还可以确认现在切换回@typescript-eslint/no-unused-vars对我来说是可行的(我更新了代码,现在一切都很好)。

This is not the way to go, and you should avoid it. @typescript-eslint/no-unused-vars-experimental is deprecated, and will be removed in the next major. Update to the latest version of the tooling and just use @typescript-eslint/no-unused-vars. Source: I am the maintainer of the project.

自2021年1月以来的最新答案

所以这是我的.eslintrc文件的最新更新

1
2
3
4
5
6
7
8
9
10
{
 "parser":"@typescript-eslint/parser",
 "extends": [
   "plugin:@typescript-eslint/recommended"
  ],
 "rules": {
   "@typescript-eslint/no-unused-vars":"error",
   "no-unused-vars":"off"
  }
}


您已将parser嵌套在parserOptions内部。它应该是一个兄弟姐妹,例如:

1
2
3
4
5
"parser":"@typescript-eslint/parser",
"parserOptions": {
   "ecmaVersion": 2018,
   "sourceType":"module"
},

对于no-unused-vars,恐怕这是@typescript-eslint的一个持续存在的错误:
https://github.com/typescript-eslint/typescript-eslint/issues/363


我的问题是使用装饰器,并希望使用一个具有适当名称的变量来表示清楚,例如:

@OneToMany((type) => Employee)而不是@OneToMany(() => Employee)

TypeScript的常见解决方案是在下划线前面加上前缀:

@OneToMany((_type) => Employee)

并且可以使ESLint接受相同的内容:

.eslintrc.js

1
2
3
4
5
6
7
module.exports = {
  ...
  rules: {
    '@typescript-eslint/no-unused-vars': ['warn', { 'argsIgnorePattern': '^_' }]
    ....
  },
};