-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathSuspiciousMethodNameDeclaration.qhelp
More file actions
53 lines (44 loc) · 1.86 KB
/
SuspiciousMethodNameDeclaration.qhelp
File metadata and controls
53 lines (44 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!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, by using the wrong keyword a programmer can accidentally declare e.g.
a method called <code>constructor</code> inside an interface.
Similarly the keyword <code>function</code> is used to declare functions in
some contexts, however, using the keyword <code>function</code> inside a class
or interface results in declaring a method called <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 an interface that is a
function.
</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>