Skip to content

Commit 903aa56

Browse files
committed
Final editing pass
1 parent d1fcf7b commit 903aa56

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

Type-Checking-JavaScript-Files.md

+9-10
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ TypeScript 2.3 and later support type-checking and reporting errors in `.js` fil
33
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`.
44
You can also ignore errors on specific lines by adding `// @ts-ignore` on the preceding line.
55

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:
77

88
## JSDoc types are used for type information
99

1010
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.
1212
Just like Typescript, `--noImplicitAny` will give you errors on the places that the compiler could not infer a type.
1313
(With the exception of open-ended object literals; see below for details.)
1414

@@ -51,7 +51,9 @@ class C {
5151
}
5252
```
5353

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.
5557
You don't even have to give a value if it will be initialised later:
5658

5759
```js
@@ -70,13 +72,11 @@ c.prop = 0; // OK
7072
c.count = "string"; // Error: string is not assignable to number|undefined
7173
```
7274

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-
7575
## Constructor functions are equivalent to classes
7676

77-
Before ES2015, Javascript used constructor functions in place of classes.
77+
Before ES2015, Javascript used constructor functions instead of classes.
7878
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.
8080

8181
```js
8282
function C() {
@@ -99,7 +99,6 @@ Similarly, `require` function calls are recognized as module imports. For exampl
9999
// same as `import module "fs"`
100100
const fs = require("fs");
101101

102-
103102
// same as `export function readFile`
104103
module.exports.readFile = function(f) {
105104
return fs.readFileSync(f);
@@ -122,7 +121,7 @@ C.D = class {
122121
}
123122
```
124123

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:
126125

127126
```js
128127
function Outer() {
@@ -209,7 +208,7 @@ It is important to note that it is an error to call a function with too many arg
209208
For instance:
210209

211210
```js
212-
function bar(a, b){
211+
function bar(a, b) {
213212
console.log(a + " " + b);
214213
}
215214

0 commit comments

Comments
 (0)