no-misused-new
Enforce valid definition of
new
andconstructor
.
Extending "plugin:@typescript-eslint/recommended"
in an ESLint configuration enables this rule.
JavaScript classes may define a constructor
method that runs when a class instance is newly created.
TypeScript allows interfaces that describe a static class object to define a new()
method (though this is rarely used in real world code).
Developers new to JavaScript classes and/or TypeScript interfaces may sometimes confuse when to use constructor
or new
.
This rule reports when a class defines a method named new
or an interface defines a method named constructor
.
module.exports = {
"rules": {
"@typescript-eslint/no-misused-new": "error"
}
};
Examples
- ❌ Incorrect
- ✅ Correct
declare class C {
new(): C;
}
interface I {
new (): I;
constructor(): void;
}
declare class C {
constructor();
}
interface I {
new (): C;
}
Options
This rule is not configurable.
When Not To Use It
If you intentionally want a class with a new
method, and you're confident nobody working in your code will mistake it with a constructor.