|
4 | 4 | <qhelp>
|
5 | 5 | <overview>
|
6 | 6 | <p>
|
7 |
| -In TypeScript the keywords <code>constructor</code> and <code>new</code> for |
8 |
| -member declarations are used to declare constructors in classes and interfaces |
9 |
| -respectively. |
10 |
| -However, a member declaration with the name <code>new</code> in an interface |
11 |
| -or <code>constructor</code> in a class, will declare an ordinary method named |
12 |
| -<code>new</code> or <code>constructor</code> rather than a constructor. |
13 |
| -Similarly, the keyword <code>function</code> is used to declare functions in |
14 |
| -some contexts. However, using the name <code>function</code> for a class |
15 |
| -or interface member declaration declares a method named <code>function</code>. |
| 7 | +In TypeScript, certain keywords have special meanings for member declarations, and misusing them can create confusion: |
| 8 | +</p> |
| 9 | + |
| 10 | +<ul> |
| 11 | +<li>In classes, use <code>constructor</code> rather than <code>new</code> to declare constructors. Using <code>new</code> within a class creates a method named "new" and not a constructor signature.</li> |
| 12 | +<li>In interfaces, use <code>new</code> rather than <code>constructor</code> to declare constructor signatures. Using <code>constructor</code> within an interface creates a method named "constructor" and not a constructor signature.</li> |
| 13 | +<li>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 "function".</li> |
| 14 | +</ul> |
| 15 | + |
| 16 | +<p> |
| 17 | +When these keywords are misused, TypeScript will interpret them as regular method names rather than their intended special syntax, leading to code that may not work as expected. |
16 | 18 | </p>
|
17 | 19 |
|
18 | 20 | </overview>
|
19 | 21 | <recommendation>
|
20 | 22 |
|
21 | 23 | <p>
|
22 |
| -Declare classes as classes and not as interfaces. |
23 |
| -Use the keyword <code>constructor</code> to declare constructors in a class, |
24 |
| -use the keyword <code>new</code> to declare constructors inside interfaces, |
25 |
| -and don't use <code>function</code> when declaring a call signature in an |
26 |
| -interface. |
| 24 | +Consider following these guidelines for clearer code: |
27 | 25 | </p>
|
28 | 26 |
|
| 27 | +<ul> |
| 28 | +<li>For classes, use <code>constructor</code> to declare constructors.</li> |
| 29 | +<li>For interfaces, use <code>new</code> to declare constructor signatures (call signatures that create new instances).</li> |
| 30 | +<li>Avoid accidentally creating methods named <code>function</code> by misusing the <code>function</code> keyword within class or interface declarations.</li> |
| 31 | +</ul> |
| 32 | + |
29 | 33 | </recommendation>
|
30 | 34 | <example>
|
31 | 35 |
|
32 | 36 | <p>
|
33 |
| -The below example declares an interface <code>Point</code> with 2 fields |
34 |
| -and a method called <code>constructor</code>. The interface does not declare |
35 |
| -a class <code>Point</code> with a constructor, which was likely what the |
36 |
| -developer meant to create. |
| 37 | +The following examples show common mistakes when using these keywords: |
37 | 38 | </p>
|
38 |
| -<sample src="examples/SuspiciousMethodNameDeclaration.ts" /> |
39 | 39 |
|
40 | 40 | <p>
|
41 |
| -The below example is a fixed version of the above, where the interface is |
42 |
| -instead declared as a class, thereby describing the type the developer meant |
43 |
| -in the first place. |
| 41 | +This interface mistakenly uses <code>constructor</code>, which creates a method named "constructor" instead of a constructor signature: |
44 | 42 | </p>
|
| 43 | +<sample src="examples/SuspiciousMethodNameDeclaration.ts" /> |
45 | 44 |
|
| 45 | +<p> |
| 46 | +Use <code>new</code> for constructor signatures in interfaces: |
| 47 | +</p> |
46 | 48 | <sample src="examples/SuspiciousMethodNameDeclarationFixed.ts" />
|
47 | 49 |
|
| 50 | +<p> |
| 51 | +This class mistakenly uses <code>new</code>, which creates a method named "new" instead of a constructor: |
| 52 | +</p> |
| 53 | +<sample src="examples/SuspiciousMethodNameDeclarationClass.ts" /> |
| 54 | + |
| 55 | +<p> |
| 56 | +Use <code>constructor</code> for constructors in classes: |
| 57 | +</p> |
| 58 | +<sample src="examples/SuspiciousMethodNameDeclarationClassFixed.ts" /> |
| 59 | + |
| 60 | +<p> |
| 61 | +This interface uses <code>function</code> as a method name, which declares a method named "function" rather than declaring a function: |
| 62 | +</p> |
| 63 | +<sample src="examples/SuspiciousMethodNameDeclarationFunction.ts" /> |
| 64 | + |
| 65 | +<p> |
| 66 | +Use a descriptive method name instead: |
| 67 | +</p> |
| 68 | +<sample src="examples/SuspiciousMethodNameDeclarationFunctionFixed.ts" /> |
| 69 | + |
48 | 70 | </example>
|
49 | 71 | <references>
|
50 | 72 |
|
| 73 | +<li>TypeScript Handbook: <a href="https://www.typescriptlang.org/docs/handbook/2/classes.html#constructors">Classes - Constructors</a>.</li> |
51 | 74 | <li>TypeScript specification: <a href="https://github.com/microsoft/TypeScript/blob/30cb20434a6b117e007a4959b2a7c16489f86069/doc/spec-ARCHIVED.md#3.8.9">Constructor Type Literals</a>.</li>
|
52 | 75 | <li>TypeScript specification: <a href="https://github.com/microsoft/TypeScript/blob/30cb20434a6b117e007a4959b2a7c16489f86069/doc/spec-ARCHIVED.md#8.3.1">Constructor Parameters</a>.</li>
|
53 | 76 |
|
|
0 commit comments