unnecessaryConstructors
Reports constructors that are equivalent to the implicit default constructor.
✅ This rule is included in the tsstylisticandstylisticStrictpresets.
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
privateorprotected, or markedpublicon 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
Examples
Section titled “Examples”class Logger { constructor() {}}class HttpError extends Error { constructor(message: string) { super(message); }}class Logger {}class HttpError extends Error { constructor(message: string) { super(message); this.name = "HttpError"; }}class Point { constructor( public x: number, public y: number, ) {}}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(); }}Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.