mirror of
https://github.com/github/codeql.git
synced 2026-01-15 07:24:49 +01:00
56 lines
2.0 KiB
XML
56 lines
2.0 KiB
XML
<!DOCTYPE qhelp PUBLIC
|
|
"-//Semmle//qhelp//EN"
|
|
"qhelp.dtd">
|
|
<qhelp>
|
|
<overview>
|
|
<p>
|
|
In TypeScript the keywords <code>constructor</code> and <code>new</code> for
|
|
member declarations are used to declare constructors in classes and interfaces
|
|
respectively.
|
|
However, a member declaration with the name <code>new</code> in an interface
|
|
or <code>constructor</code> in a class, will declare an ordinary method named
|
|
<code>new</code> or <code>constructor</code> rather than a constructor.
|
|
Similarly, the keyword <code>function</code> is used to declare functions in
|
|
some contexts. However, using the name <code>function</code> for a class
|
|
or interface member declaration declares a method named <code>function</code>.
|
|
</p>
|
|
|
|
</overview>
|
|
<recommendation>
|
|
|
|
<p>
|
|
Declare classes as classes and not as interfaces.
|
|
Use the keyword <code>constructor</code> to declare constructors in a class,
|
|
use the keyword <code>new</code> to declare constructors inside interfaces,
|
|
and don't use <code>function</code> when declaring a call signature in an
|
|
interface.
|
|
</p>
|
|
|
|
</recommendation>
|
|
<example>
|
|
|
|
<p>
|
|
The below example declares an interface <code>Point</code> with 2 fields
|
|
and a method called <code>constructor</code>. The interface does not declare
|
|
a class <code>Point</code> with a constructor, which was likely what the
|
|
developer meant to create.
|
|
</p>
|
|
<sample src="examples/SuspiciousMethodNameDeclaration.ts" />
|
|
|
|
<p>
|
|
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.
|
|
</p>
|
|
|
|
<sample src="examples/SuspiciousMethodNameDeclarationFixed.ts" />
|
|
|
|
</example>
|
|
<references>
|
|
|
|
<li>TypeScript specification: <a href="https://github.com/microsoft/TypeScript/blob/master/doc/spec.md#3.8.9">Constructor Type Literals</a>.</li>
|
|
<li>TypeScript specification: <a href="https://github.com/microsoft/TypeScript/blob/master/doc/spec.md#8.3.1">Constructor Parameters</a>.</li>
|
|
|
|
</references>
|
|
</qhelp>
|