Skip to content

unnecessaryConstructors

Reports constructors that are equivalent to the implicit default constructor.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

Classes that don't declare a constructor receive an implicit one automatically:

  • Base classes get an empty constructor, equivalent to constructor() {}.
  • Derived classes get a constructor that forwards all arguments to the parent class, equivalent to constructor(...args) { super(...args); }.

Writing either of those constructors out manually adds code to read and maintain without changing how the class works.

Constructors that do change behavior are not reported, including:

  • Constructors marked private or protected, or marked public on a derived class, as those modifiers change who may instantiate the class
  • Constructors with parameter properties such as constructor(private name: string), as those declare and initialize class fields
  • Constructors with parameter decorators, as those can have side effects
  • Constructor overload signatures, which declare types rather than behavior
class Logger {
constructor() {}
}
class HttpError extends Error {
constructor(message: string) {
super(message);
}
}

Constructors that exist only to widen the visibility of a parent class's protected constructor should declare an explicit public modifier. The rule treats an explicit public on a derived class as intentional and does not report it:

class Base {
protected constructor() {}
}
class Derived extends Base {
public constructor() {
super();
}
}

This rule is not configurable.

If your project deliberately declares an explicit constructor on every class, such as to serve as a documented extension point or to keep generated code uniform, you may not want this rule. You might consider using Flint disable comments and/or configuration file disables for those specific situations instead of completely disabling this rule.

Made with ❤️‍🔥 around the world by the Flint team and contributors.