You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: Type-Checking-JavaScript-Files.md
+9-10
Original file line number
Diff line number
Diff line change
@@ -3,12 +3,12 @@ TypeScript 2.3 and later support type-checking and reporting errors in `.js` fil
3
3
You can skip checking some files by adding `// @ts-nocheck` comment to them; conversely, you can choose to check only a few `.js` files by adding a `// @ts-check` comment to them without setting `--checkJs`.
4
4
You can also ignore errors on specific lines by adding `// @ts-ignore` on the preceding line.
5
5
6
-
Here are some notable differences on how checking work in `.js`file from `.ts`file:
6
+
Here are some notable differences on how checking works in `.js`files compared to `.ts`files:
7
7
8
8
## JSDoc types are used for type information
9
9
10
10
In a `.js` file, types can often be inferred just like in `.ts` files.
11
-
Likewise, when types can't be inferred, they can be specified using JSDoc the same way that type annotations do in a `.ts` file.
11
+
Likewise, when types can't be inferred, they can be specified using JSDoc the same way that type annotations are used in a `.ts` file.
12
12
Just like Typescript, `--noImplicitAny` will give you errors on the places that the compiler could not infer a type.
13
13
(With the exception of open-ended object literals; see below for details.)
14
14
@@ -51,7 +51,9 @@ class C {
51
51
}
52
52
```
53
53
54
-
If the property type can't be inferred, annotate the assignment in the constructor with JSDoc to specify the type.
54
+
55
+
If properties are never set in the class body, they are considered unknown.
56
+
If your class has properties that are only read from, add and then annotate a declaration in the constructor with JSDoc to specify the type.
55
57
You don't even have to give a value if it will be initialised later:
56
58
57
59
```js
@@ -70,13 +72,11 @@ c.prop = 0; // OK
70
72
c.count="string"; // Error: string is not assignable to number|undefined
71
73
```
72
74
73
-
If properties are never set in the class body, they are considered unknown. If your class has properties that are only read from, consider adding an initialization in the constructor to undefined, e.g. `this.prop = undefined;`.
74
-
75
75
## Constructor functions are equivalent to classes
76
76
77
-
Before ES2015, Javascript used constructor functions in place of classes.
77
+
Before ES2015, Javascript used constructor functions instead of classes.
78
78
The compiler supports this pattern and understands constructor functions as equivalent to ES2015 classes.
79
-
The property inferences rules described above work exactly the same way.
79
+
The property inference rules described above work exactly the same way.
80
80
81
81
```js
82
82
functionC() {
@@ -99,7 +99,6 @@ Similarly, `require` function calls are recognized as module imports. For exampl
99
99
// same as `import module "fs"`
100
100
constfs=require("fs");
101
101
102
-
103
102
// same as `export function readFile`
104
103
module.exports.readFile=function(f) {
105
104
returnfs.readFileSync(f);
@@ -122,7 +121,7 @@ C.D = class {
122
121
}
123
122
```
124
123
125
-
And, for pre-ES2015 code, it can be used to simulate `static` methods:
124
+
And, for pre-ES2015 code, it can be used to simulate static methods:
126
125
127
126
```js
128
127
functionOuter() {
@@ -209,7 +208,7 @@ It is important to note that it is an error to call a function with too many arg
0 commit comments