'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?
Solution 1:[1]
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:
- 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)
- Add a
project
option in myparserOptions
which points to thetsconfig
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.
Solution 2:[2]
For stop this error
write this on .eslintrc file
"rules": {
"no-useless-constructor": 0,
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | anotheruser |
Solution 2 |