ESlint抱怨无用的构造函数

时间:2020-07-26 08:19:06

标签: typescript eslint

此文件中出现ESlint错误

Bar.ts:

class Bar {
  constructor(public foo: string) {}

  hello(): string {
    return this.foo;
  }
}

export default Bar;

我的eslint配置:

{
  "env": {
    "browser": true,
    "commonjs": true,
    "es2020": true
  },
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "airbnb/base",
    "eslint-config-prettier"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": 11
  },
  "plugins": ["@typescript-eslint/eslint-plugin", "eslint-plugin-prettier"],
  "rules": {
    "prettier/prettier": "error",
    "no-unused-vars": "warn",
    "@typescript-eslint/no-var-requires": "warn",
    "func-names": "off",
    "no-underscore-dangle": "off",
    "class-methods-use-this": "off"
  },
  "overrides": [
    {
      "files": ["*.ts"],
      "excludedFiles": ["*.js"],
      "rules": {
        "@typescript/no-var-requires": "off"
      }
    }
  ]
}

Bar.ts上的Eslint错误:

Useless constructor. eslint(no-useless-constructor)

如果您想知道为什么我的eslint配置看起来是这样,那是因为我试图设置一个同时包含JS和TS文件的项目,并使ESLint分别适用于每种文件类型。

我需要在ESLint配置中进行哪些更改,以使其不抱怨完全相关的代码?

1 个答案:

答案 0 :(得分:1)

由于@jonrsharpe,我发现问题出在extends块内。 airbnb/base特别适用于js文件。 我不得不将其替换为airbnb-typescript/base,然后弹出另一个更为关键的问题:

要使用airbnb-typescript / base,我必须:

  1. 生成tsconfig(我尚未生成它,因为当前的主要目标是使ESlint或多或少正确地工作并完全忘记一些基本知识)
  2. 在我的project中添加一个parserOptions选项,该选项指向tsconfig

熟悉TS和ESlint的人可能从一开始就注意到我的eslint配置缺少该选项。

我很确定我在这里遗漏了很多东西,这不是关于此主题的最后一个问题,但是现在错误已经消失,我可以继续前进。