noUselessConstructor
Diagnostic Category: lint/complexity/noUselessConstructor
Since: v1.0.0
Sources:
- Same as:
no-useless-constructor
- Same as:
@typescript-eslint/no-useless-constructor
Disallow unnecessary constructors.
ES2015 provides a default class constructor if one is not specified. As such, providing an empty constructor or one that delegates into its parent is unnecessary.
The rule ignores:
- decorated classes;
- constructors with at least one parameter property;
private
andprotected
constructors.
Caveat
Section titled CaveatThis rule reports on constructors whose sole purpose is to make a parent constructor public. See the last invalid example.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.js:2:5 lint/complexity/noUselessConstructor FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This constructor is unnecessary.
1 │ class A {
> 2 │ constructor (a) {}
│ ^^^^^^^^^^^^^^^^^^
3 │ }
4 │
ℹ Unsafe fix: Remove the unnecessary constructor.
1 1 │ class A {
2 │ - ····constructor·(a)·{}
3 2 │ }
4 3 │
code-block.ts:2:5 lint/complexity/noUselessConstructor FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This constructor is unnecessary.
1 │ class B extends A {
> 2 │ constructor (a) {
│ ^^^^^^^^^^^^^^^^^
> 3 │ super(a);
> 4 │ }
│ ^
5 │ }
6 │
ℹ Unsafe fix: Remove the unnecessary constructor.
1 1 │ class B extends A {
2 │ - ····constructor·(a)·{
3 │ - ········super(a);
4 │ - ····}
5 2 │ }
6 3 │
code-block.js:5:5 lint/complexity/noUselessConstructor FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This constructor is unnecessary.
3 │ * Documented constructor.
4 │ */
> 5 │ constructor () {}
│ ^^^^^^^^^^^^^^^^^
6 │ }
7 │
ℹ Unsafe fix: Remove the unnecessary constructor.
1 1 │ class C {
2 │ - ····/**
3 │ - ·····*·Documented·constructor.
4 │ - ·····*/
5 │ - ····constructor·()·{}
6 2 │ }
7 3 │
code-block.js:2:5 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ ‘protected’ modifier can only be used in TypeScript files
1 │ class A {
> 2 │ protected constructor() {
│ ^^^^^^^^^
3 │ this.prop = 1;
4 │ }