noMisleadingInstantiator
Ce contenu n’est pas encore disponible dans votre langue.
Diagnostic Category: lint/suspicious/noMisleadingInstantiator
Since: v1.3.0
Sources:
- Same as:
@typescript-eslint/no-misused-new
Enforce proper usage of new
and constructor
.
In JavaScript, classes utilize the constructor
method to initialize a new instance. On the other hand, TypeScript interfaces can describe a class type with a new()
method signature, though this pattern is not commonly seen in real-world code. Developers, especially those new to JavaScript or TypeScript, might occasionally confuse the use of constructor
with new
.
This rule triggers warnings in the following scenarios:
- When a class has a method named
new
. - When an interface defines a method named
constructor
ornew
that returns the interface type. - When a type alias has a
constructor
method.
You should not use this rule if you intentionally want a class with a new
method, and you’re confident nobody working in your code will mistake it with an constructor
.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.ts:2:3 lint/suspicious/noMisleadingInstantiator ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Don’t use the new method in interfaces.
1 │ interface I {
> 2 │ new (): I;
│ ^^^^^^^^^^
3 │ constructor(): void;
4 │ }
ℹ new in an interface suggests it’s instantiable, which is incorrect. The returned type should different from the constructor’s type.
code-block.ts:2:3 lint/suspicious/noMisleadingInstantiator ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Don’t use the new method in classes.
1 │ class C {
> 2 │ new(): C;
│ ^^^^^^^^^
3 │ }
4 │
ℹ new is typically used to instantiate objects. In classes, its usage can be misleading.