Skip to content

noUselessConstructor (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"complexity": {
"noUselessConstructor": "error"
}
}
}
}

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 and protected constructors;
  • TypeScript constructors that forward at least one argument to super.

TypeScript forwarding constructors can narrow the parameter types accepted by a subclass. The rule does not compare parent and child signatures, so it ignores these constructors even when the signatures are identical.

This rule reports on zero-argument constructors whose sole purpose is to make a parent constructor public. See the last invalid example.

class A {
constructor (a) {}
}
code-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 │

class B extends A {
constructor (a) {
super(a);
}
}
code-block.js: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 │

class C {
/**
* Documented constructor.
*/
constructor () {}
}
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 │

class A {
protected constructor() {
this.prop = 1;
}
}
class B extends A {
// Make the parent constructor public.
constructor () {
super();
}
}
code-block.ts:9:5 lint/complexity/noUselessConstructor  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This constructor is unnecessary.

7 │ class B extends A {
8 │ // Make the parent constructor public.
> 9 │ constructor () {
│ ^^^^^^^^^^^^^^^^
> 10 │ super();
> 11 │ }
│ ^
12 │ }
13 │

ℹ Unsafe fix: Remove the unnecessary constructor.

6 6 │
7 7 │ class B extends A {
8 │ - ····//·Make·the·parent·constructor·public.
9 │ - ····constructor·()·{
10 │ - ········super();
11 │ - ····}
12 8 │ }
13 9 │

class A {
constructor (prop) {
this.prop = prop;
}
}
class B extends A {
constructor () {
super(5);
}
}
class C {
// Empty constructor with parameter properties are allowed.
constructor (private prop: number) {}
}
class D {
constructor(public arg: number){}
}
class F extends D {
// constructor with default parameters are allowed.
constructor(arg = 4) {
super(arg)
}
}
class Base {
constructor(public value: string | number) {}
}
class Narrowed extends Base {
constructor(value: string) {
super(value);
}
}
@Decorator
class C {
constructor (prop: number) {}
}