esLint-为打字稿配置“ no-unused-vars”

Rin*_*ins 5 typescript eslint

我在具有以下设置的所有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的目的。

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

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)

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

  • 这应该是当前公认的解决方案。添加“no-unused-vars”和“@typescript-eslint/no-unused-vars”规则对我有用。接受的解决方案没有检测到未使用的 var 错误。 (8认同)
  • 这只是禁用规则。它与它是一个接口无关。所以这只是关闭这个有用的规则。这不是你想做的 (4认同)
  • 引用问题中的OP *我不喜欢禁用该规则* (4认同)

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现在对我有用(我更新了我的代码,现在一切都很好)。

这不是要走的路,你应该避免它。@typescript-eslint/no-unused-vars-experimental已弃用,并将在下一个专业中删除。更新到最新版本的工具,只需使用@typescript-eslint/no-unused-vars. 来源:我是该项目的维护者。

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

所以.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)

  • 这是唯一对我有用的解决方案。我将 xo 与 Typescript 一起使用,并不断收到所有 no-unused-vars 错误。 (4认同)
  • @BradZacher 不幸的是,我仍然遇到同样的问题,其中导入的接口用作返回类型,触发此规则。我使用以下版本:“@typescript-eslint/eslint-plugin”:“4.14.1”,“@typescript-eslint/parser”:“4.14.1”,“eslint”:“7.18.0”, (4认同)
  • 这不是要走的路,你应该避免它。`@typescript-eslint/no-unused-vars-experimental` 已弃用,并将在下一个主要版本中删除。更新到最新版本的工具并仅使用“@typescript-eslint/no-unused-vars”。来源:我是该项目的维护者。 (3认同)

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)

  • 捕获各种声明的规则是: `"@typescript-eslint/no-unused-vars": ["error", {argsIgnorePattern: "^_", destructedArrayIgnorePattern: "^_", varsIgnorePattern: "^_ “}]`(来源:https://eslint.org/docs/latest/rules/no-unused-vars#options) (7认同)

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)


Jay*_*sch 5

您已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.

  • 我觉得这个答案已经过时了。我已经在 `eslintrc.json` 的 `extends` 部分中有 `"plugin:@typescript-eslint/recommended"`,所以我添加了 `"eslint:recommished"` 和 `"plugin:@typescript-eslint/eslint-recommended" ` 以正确的顺序,但 eslint 仍然无法检测到 vscode 正在检测的未使用的 var 错误。使用@jaibatrik 的解决方案或@ghiscoding 的更新解决方案是有效的。 (4认同)
  • 该订单对我来说很重要。确保 `"plugin:@typescript-eslint/recommended"` 位于其他扩展之后 (3认同)
  • 是的,这似乎确实解决了这个问题。正如您所说,它确实与我的其他一些扩展发生冲突,但一旦我有时间进行实验,我应该能够弄清楚这一点。非常感谢! (2认同)

sup*_*ate 5

从 3.x升级@typescript-eslint/eslint-plugin到@typescript-eslint/parser最新的 4.x 解决了我的问题。