ESLint - "no-unused-vars" 每次导入的警告

ESLint - "no-unused-vars" warning for every import

我正在使用一个 Angular 项目,只是想再次将 ESLint 与 Prettier 结合使用。可悲的是,有一个烦人的问题,每次导入都会显示警告 'XYZ' is defined but never used. eslint(@typescript-eslint/no-unused-vars)

只有完全禁用此规则才能解决此问题。但是我不会像第 22 行那样得到未使用的 const 变量的提示。

我的eslintrc.json:

{
  "parser": "@typescript-eslint/parser", // Specifies the ESLint parser
  "extends": [
    "plugin:angular/johnpapa", //ESLint rules for your angular project with checks for best-practices, conventions or potential errors.
    "plugin:@typescript-eslint/recommended", // Uses the recommended rules from the @typescript-eslint/eslint-plugin
    "prettier", // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
    "plugin:prettier/recommended" // Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
  ],
  "parserOptions": {
    "ecmaVersion": 2020, // Allows for the parsing of modern ECMAScript features
    "sourceType": "module" // Allows for the use of imports
  },
  "settings": {
    "angular": {
      "version": "detect" // Tells eslint-plugin-react to automatically detect the version of React to use
    }
  },
  "root": true,
  "env": {
    "node": true,
    "jest": true
  },
  "rules": {
    // Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
    "no-empty-function": "off",
    "@typescript-eslint/no-empty-function": ["off"],
  },
  "ignorePatterns": ["/*.*"]
}

使用的依赖项:

"@typescript-eslint/eslint-plugin": "^4.28.4",
"eslint": "^7.31.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-angular": "^4.0.1",
"eslint-plugin-prettier": "^3.4.0",
"prettier": "^2.3.2",
"prettier-eslint": "^12.0.0",

好像有些版本有问题,还少了一个包。包 "@typescript-eslint/parser": "^4.19.0".

它现在可以与 devDependencies 中的以下软件包一起使用:

"@typescript-eslint/eslint-plugin": "^4.19.0",
"@typescript-eslint/parser": "^4.19.0",
"eslint": "^7.23.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-angular": "^4.0.1",
"prettier": "^2.3.2",
"prettier-eslint": "^12.0.0"

在此之前,eslint 包的版本甚至更高:

"eslint-config-prettier": "^8.3.0",
"eslint-plugin-angular": "^4.0.1",
"eslint-plugin-prettier": "^3.4.0"

所以我使用 npm 命令 npm update --save-dev 更新所有开发依赖项,降级列出的依赖项并使其工作。