@typescript-eslint/no-unused-vars 类型声明中的误报

Ale*_*vey 22 javascript typescript reactjs eslint typescript-eslint

@typescript-eslint/no-unused-vars存在一个问题。所以,我们有类型

type SomeType = (name: string) => void;

Run Code Online (Sandbox Code Playgroud)

我们在带有类型声明的字符串中有@typescript-eslint/no-unused-vars错误,它说 'name' is defined but never used

类型用法示例:

export const LogSomeInfo: SomeType = (name: string) => {
    const a = name;
    console.log(a);
};

Run Code Online (Sandbox Code Playgroud)

或者:

interface CheckboxPropsType {
    value: string,
    onClick(value: string): void
}
Run Code Online (Sandbox Code Playgroud)

并且 eslint 在 onClick... 字符串处中断,表示该值已定义但从未使用过。即使类型分配正确且实际的 onClick 处理程序使用该值!

问题:这条规则有什么问题,为什么会在这种情况下触发。为什么 eslint 将类型/接口中函数的类型声明识别为常规函数?我的 eslint 配置有问题吗?

"eslint": "^7.7.0", "@typescript-eslint/eslint-plugin": "^3.6.1", "@typescript-eslint/parser": "^4.0.1", "eslint-config- airbnb 打字稿": "^9.0.0",

{
  "extends": [
    "airbnb-typescript",
    "airbnb/hooks",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 2018,
    "sourceType": "module",
    "project": "./tsconfig.json"
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  },
  "plugins": ["@typescript-eslint", "react-hooks", "react"],
  "env": {
    "browser": true
  },
  "rules": {
    "object-curly-newline": 0,
    "import/named": ["error"],
    "indent": ["error", 4],
    "react/jsx-indent": ["error", 4],
    "comma-dangle": ["error", "never"],
    "import/prefer-default-export": "off",
    "react/jsx-fragments": "off",
    "arrow-body-style": "off",
    "object-curly-spacing": "off",
    "@typescript-eslint/indent": ["error", 4, {"SwitchCase": 1}],
    "@typescript-eslint/explicit-module-boundary-types": "off",
    "no-undef": "error",
    "react/jsx-indent-props": ["error", 4],
    "max-len": ["error", { "code": 120 }],
    "react/prop-types": "off"
  }
}

Run Code Online (Sandbox Code Playgroud)

Bra*_*her 23

来源:我是该typescript-eslint项目的维护者。

如果您将@typescript-eslint/parser和版本更新@typescript-eslint/eslint-plugin到 v4.1.0,您将能够使用最新的更改,这些更改可以@typescript-eslint/no-unused-vars在所有情况下正常工作。


顺便说一句 - 使用插件的 v3.x 但解析器的 v4.x 会让你进入一个非常奇怪的状态,具有未定义和不受支持的行为。

您应该确保始终使用两个软件包的相同版本,因为每个版本都是一起发布的。

  • 我使用的是“4.22.1”版本,但仍然收到警告。是否需要任何其他配置来禁用“类型”或“接口”中函数参数的 linting? (10认同)
  • 对于遇到问题甚至对我的回复投反对票的人 - 请使用您的配置在 github 上提交问题,以便我可以帮助您 - https://github.com/typescript-eslint/typescript-eslint。投反对票/评论“它不起作用”对于解决您的问题毫无用处。--- 当我说我想帮助您解决问题时,我并不是在开玩笑。如果这是一个错误我会修复它。如果是配置问题我会帮你解决。通过使用您的代码+配置提交 GH 问题来帮助我帮助您。 (7认同)
  • 我的版本都是“4.29.0”,但仍然面临这个问题。 (6认同)
  • 截至撰写本文时,当前版本“5.2.0”仍然出现误报。我还应该提到,我在 Vue 项目中使用“@typescript-eslint/*”。 (4认同)
  • 这是今天更好的解决方案。确认在 4.20.0 上运行 (2认同)

Dol*_*lly 16

之前检查过的类似问题:Why ESLint throws 'no-unused-vars' for TypeScript interface?

禁用'@typescript-eslint/no-unused-vars': 0规则,而使用"strict": true"noUnusedLocals": true以及"noUnusedParameters": true在tsconfig.json

  • `no-unused-vars-experimental` 是实验性的,现已被弃用。它将在下一个主要版本中被删除 - 不要使用它。如果您想使用 TS 内置的未使用检查或 eslint 规则,由您决定。TS 检查会阻止您的构建,因此很多人不喜欢它们。请参阅下面我的回答,了解如何修复 eslint 配置。 (2认同)

小智 8

只需确保您拥有以下最新版本:

  • typescript-eslint
  • @typescript-eslint/解析器
  • @typescript-eslint/eslint-插件

在.eslintrc.js中,确保将以下行添加到规则部分:

  "no-unused-vars": "off",
  "@typescript-eslint/no-unused-vars": ["error"]
Run Code Online (Sandbox Code Playgroud)