Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESlint complaining about useless constructor

I'm getting ESlint error in this file

Bar.ts:

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

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

export default Bar;

my eslint config:

{
  "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"
      }
    }
  ]
}

Eslint error on Bar.ts:

Useless constructor. eslint(no-useless-constructor)

If you are curious why my eslint config looks the way it is, it's because I'm trying to setup a project with both JS and TS files, and make ESLint work for each type of the file respectively.

What do I need to change in my ESLint config to make it not complaining about totally relevant code?

like image 553
anotheruser Avatar asked Jul 26 '20 08:07

anotheruser


1 Answers

Thanks to @jonrsharpe I have figured out that that the issue was inside the extends block. The airbnb/base in particular which is meant for js files. I had to replace it with airbnb-typescript/base and then the other issue popped up which was even more critical:

To use airbnb-typescript/base I had to:

  1. Generate the tsconfig (I haven't generated it yet, since currently my primary goal is to get ESlint to work more or less properly and completetly forgot about some basic things)
  2. Add a project option in my parserOptions which points to the tsconfig

Somebody who is familiar with the TS and ESlint probably has noticed from the very beginning that my eslint config was missing that option.

I'm pretty sure I'm missing a lot of things here and this is not the last question about this topic, but for now the error is gone and I can move forward.

like image 66
anotheruser Avatar answered Oct 09 '22 23:10

anotheruser