简体   繁体   English

ESLint - TypeScript 类型/接口中 function 参数上的“no-unused-vars”

[英]ESLint - "no-unused-vars" on function parameters within TypeScript Types/Interfaces

I am currently getting a eslint warning on function parameters within Types or Interfaces .我目前在TypesInterfaces中收到有关 function 参数的 eslint 警告。

Doing the following:执行以下操作:

type Test = {
  fn: (param: string) => void
}

Results in the following warning:导致以下警告:

'param' is defined but never used. Allowed unused args must match /^_/u.

Here's what my .eslintrc.json looks like:这是我的.eslintrc.json的样子:

{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "tsconfig.json"
  },
  "env": {
    "node": true
  },
  "plugins": ["@typescript-eslint"],
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:prettier/recommended"
  ],
  "rules": {
    "@typescript-eslint/interface-name-prefix": "off",
    "@typescript-eslint/explicit-function-return-type": "off",
    "@typescript-eslint/explicit-module-boundary-types": "off"
  }
}

Package versions: Package 版本:

"eslint": "^7.23.0"
"@typescript-eslint/eslint-plugin": "^4.22.1"
"@typescript-eslint/parser": "^4.22.1"

Similar questions have been asked already here and here , but the provided answers do not seem to be doing it for me.已经在这里这里提出了类似的问题,但提供的答案似乎对我没有用。

Any suggestions as to what could be happening?关于可能发生什么的任何建议?

I solved it this way, put these two lines in rules in the eslint configuration file.我是这样解决的,把这两行放在eslint配置文件的rules里面。

"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error"]

Instead of setting the no-unused-vars globally in your eslint config, you could set it to the line by placing this comment just above it:无需在 eslint 配置中全局设置no-unused-vars ,您可以通过在其上方放置以下注释来将其设置为该行:

 // eslint-disable-next-line @typescript-eslint/no-unused-vars

or to the entire file, just add this comment on the top:或整个文件,只需在顶部添加此评论:

/* eslint-disable @typescript-eslint/no-unused-vars */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM