CodeQL documentation

Suspicious method name declaration

ID: js/suspicious-method-name-declaration
Kind: problem
Severity: warning
Precision: high
Tags:
   - correctness
   - typescript
   - methods
Query suites:
   - javascript-security-and-quality.qls

Click to see the query in the CodeQL repository

In TypeScript the keywords constructor and new for member declarations are used to declare constructors in classes and interfaces respectively. However, a member declaration with the name new in an interface or constructor in a class, will declare an ordinary method named new or constructor rather than a constructor. Similarly, the keyword function is used to declare functions in some contexts. However, using the name function for a class or interface member declaration declares a method named function.

Recommendation

Declare classes as classes and not as interfaces. Use the keyword constructor to declare constructors in a class, use the keyword new to declare constructors inside interfaces, and don’t use function when declaring a call signature in an interface.

Example

The below example declares an interface Point with 2 fields and a method called constructor. The interface does not declare a class Point with a constructor, which was likely what the developer meant to create.

declare class Point {
   x: number;
   y: number;
   constructor(x : number, y: number);
}

The below example is a fixed version of the above, where the interface is instead declared as a class, thereby describing the type the developer meant in the first place.

interface Point {
   x: number;
   y: number;
}

References