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: packages/documentation/copy/en/declaration-files/Publishing.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ There are two main ways you can publish your declaration files to npm:
11
11
1. bundling with your npm package
12
12
2. publishing to the [@types organization](https://www.npmjs.com/~types) on npm.
13
13
14
-
If your types are generated by your source code, publish the types with your source code. Both TypeScript and JavaScript projects can generate types via [`--declaration`](/tsconfig#declaration).
14
+
If your types are generated by your source code, publish the types with your source code. Both TypeScript and JavaScript projects can generate types via [`declaration`](/tsconfig#declaration).
15
15
16
16
Otherwise, we recommend submitting the types to DefinitelyTyped, which will publish them to the `@types` organization on npm.
17
17
@@ -31,9 +31,9 @@ For example:
31
31
}
32
32
```
33
33
34
-
Note that the `"typings"` field is synonymous with `"types"`, and could be used as well.
34
+
Note that the `"typings"` field is synonymous with `types`, and could be used as well.
35
35
36
-
Also note that if your main declaration file is named `index.d.ts` and lives at the root of the package (next to `index.js`) you do not need to mark the `"types"` property, though it is advisable to do so.
36
+
Also note that if your main declaration file is named `index.d.ts` and lives at the root of the package (next to `index.js`) you do not need to mark the `types` property, though it is advisable to do so.
Copy file name to clipboardExpand all lines: packages/documentation/copy/en/handbook-v1/Basic Types.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -274,7 +274,7 @@ function warnUser(): void {
274
274
}
275
275
```
276
276
277
-
Declaring variables of type `void` is not useful because you can only assign `null` (only if `--strictNullChecks` is not specified, see next section) or `undefined` to them:
277
+
Declaring variables of type `void` is not useful because you can only assign `null` (only if [`strictNullChecks`](/tsconfig#strictNullChecks) is not specified, see next section) or `undefined` to them:
278
278
279
279
```ts twoslash
280
280
// @strict: false
@@ -297,13 +297,13 @@ let n: null = null;
297
297
By default `null` and `undefined` are subtypes of all other types.
298
298
That means you can assign `null` and `undefined` to something like `number`.
299
299
300
-
However, when using the `--strictNullChecks` flag, `null` and `undefined` are only assignable to `unknown`, `any` and their respective types (the one exception being that `undefined` is also assignable to `void`).
300
+
However, when using the [`strictNullChecks`](/tsconfig#strictNullChecks) flag, `null` and `undefined` are only assignable to `unknown`, `any` and their respective types (the one exception being that `undefined` is also assignable to `void`).
301
301
This helps avoid _many_ common errors.
302
302
In cases where you want to pass in either a `string` or `null` or `undefined`, you can use the union type `string | null | undefined`.
303
303
304
304
Union types are an advanced topic that we'll cover in a later chapter.
305
305
306
-
> As a note: we encourage the use of `--strictNullChecks` when possible, but for the purposes of this handbook, we will assume it is turned off.
306
+
> As a note: we encourage the use of [`strictNullChecks`](/tsconfig#strictNullChecks) when possible, but for the purposes of this handbook, we will assume it is turned off.
Copy file name to clipboardExpand all lines: packages/documentation/copy/en/handbook-v2/Basics.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -280,7 +280,7 @@ Why should converting it over to TypeScript stop you from running it?
280
280
281
281
So TypeScript doesn't get in your way.
282
282
Of course, over time, you may want to be a bit more defensive against mistakes, and make TypeScript act a bit more strictly.
283
-
In that case, you can use the `--noEmitOnError` compiler option.
283
+
In that case, you can use the [`noEmitOnError`](/tsconfig#noEmitOnError) compiler option.
284
284
Try changing your `hello.ts` file and running `tsc` with that flag:
285
285
286
286
```sh
@@ -392,7 +392,7 @@ TypeScript has the ability to rewrite code from newer versions of ECMAScript to
392
392
This process of moving from a newer or "higher" version of ECMAScript down to an older or "lower" one is sometimes called _downleveling_.
393
393
394
394
By default TypeScript targets ES3, an extremely old version of ECMAScript.
395
-
We could have chosen something a little bit more recent by using the `--target` flag.
395
+
We could have chosen something a little bit more recent by using the [`target`](/tsconfig#target) option.
396
396
Running with `--target es2015` changes TypeScript to target ECMAScript 2015, meaning code should be able to run wherever ECMAScript 2015 is supported.
397
397
So running `tsc --target es2015 hello.ts` gives us the following output:
398
398
@@ -421,8 +421,8 @@ This can require a little extra work, but generally speaking it pays for itself
421
421
When possible, a new codebase should always turn these strictness checks on.
422
422
423
423
TypeScript has several type-checking strictness flags that can be turned on or off, and all of our examples will be written with all of them enabled unless otherwise stated.
424
-
The `--strict` flag in the CLI, or `"strict": true` in a [`tsconfig.json`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) toggles them all on simultaneously, but we can opt out of them individually.
425
-
The two biggest ones you should know about are `noImplicitAny` and `strictNullChecks`.
424
+
The [`strict`](/tsconfig#strict) flag in the CLI, or `"strict": true` in a [`tsconfig.json`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) toggles them all on simultaneously, but we can opt out of them individually.
425
+
The two biggest ones you should know about are [`noImplicitAny`](/tsconfig#noImplicitAny) and [`strictNullChecks`](/tsconfig#strictNullChecks).
426
426
427
427
## `noImplicitAny`
428
428
@@ -431,10 +431,10 @@ This isn't the worst thing that can happen - after all, falling back to `any` is
431
431
432
432
However, using `any` often defeats the purpose of using TypeScript in the first place.
433
433
The more typed your program is, the more validation and tooling you'll get, meaning you'll run into fewer bugs as you code.
434
-
Turning on the `noImplicitAny` flag will issue an error on any variables whose type is implicitly inferred as `any`.
434
+
Turning on the [`noImplicitAny`](/tsconfig#noImplicitAny) flag will issue an error on any variables whose type is implicitly inferred as `any`.
435
435
436
436
## `strictNullChecks`
437
437
438
438
By default, values like `null` and `undefined` are assignable to any other type.
439
439
This can make writing some code easier, but forgetting to handle `null` and `undefined` is the cause of countless bugs in the world - some consider it a [billion dollar mistake](https://www.youtube.com/watch?v=ybrQvs4x0Ps)!
440
-
The `strictNullChecks` flag makes handling `null` and `undefined` more explicit, and _spares_ us from worrying about whether we _forgot_ to handle `null` and `undefined`.
440
+
The [`strictNullChecks`](/tsconfig#strictNullChecks) flag makes handling `null` and `undefined` more explicit, and _spares_ us from worrying about whether we _forgot_ to handle `null` and `undefined`.
Copy file name to clipboardExpand all lines: packages/documentation/copy/en/handbook-v2/Classes.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -69,7 +69,7 @@ pt.x = "0";
69
69
70
70
#### `--strictPropertyInitialization`
71
71
72
-
The `strictPropertyInitialization` setting controls whether class fields need to be initialized in the constructor.
72
+
The [`strictPropertyInitialization`](/tsconfig#strictPropertyInitialization) setting controls whether class fields need to be initialized in the constructor.
Copy file name to clipboardExpand all lines: packages/documentation/copy/en/handbook-v2/Everyday Types.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -635,17 +635,17 @@ The `as const` suffix acts like `const` but for the type system, ensuring that a
635
635
636
636
JavaScript has two primitive values used to signal absent or uninitialized value: `null` and `undefined`.
637
637
638
-
TypeScript has two corresponding _types_ by the same names. How these types behave depends on whether you have the `strictNullChecks` option on.
638
+
TypeScript has two corresponding _types_ by the same names. How these types behave depends on whether you have the [`strictNullChecks`](/tsconfig#strictNullChecks) option on.
639
639
640
640
### `strictNullChecks` off
641
641
642
-
With `strictNullChecks`_off_, values that might be `null` or `undefined` can still be accessed normally, and the values `null` and `undefined` can be assigned to a property of any type.
642
+
With [`strictNullChecks`](/tsconfig#strictNullChecks)_off_, values that might be `null` or `undefined` can still be accessed normally, and the values `null` and `undefined` can be assigned to a property of any type.
643
643
This is similar to how languages without null checks (e.g. C#, Java) behave.
644
-
The lack of checking for these values tends to be a major source of bugs; we always recommend people turn `strictNullChecks` on if it's practical to do so in their codebase.
644
+
The lack of checking for these values tends to be a major source of bugs; we always recommend people turn [`strictNullChecks`](/tsconfig#strictNullChecks) on if it's practical to do so in their codebase.
645
645
646
646
### `strictNullChecks` on
647
647
648
-
With `strictNullChecks`_on_, when a value is `null` or `undefined`, you will need to test for those values before using methods or properties on that value.
648
+
With [`strictNullChecks`](/tsconfig#strictNullChecks)_on_, when a value is `null` or `undefined`, you will need to test for those values before using methods or properties on that value.
649
649
Just like checking for `undefined` before using an optional property, we can use _narrowing_ to check for values that might be `null`:
Copy file name to clipboardExpand all lines: packages/documentation/copy/en/handbook-v2/Modules.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ Conversely, to consume a variable, function, class, interface, etc. exported fro
27
27
Before we start, it's important to understand what TypeScript considers a module.
28
28
The JavaScript specification declares that any JavaScript files without an `export` or top-level `await` should be considered a script and not a module.
29
29
30
-
Inside a script file variables and types are declared to be in the shared global scope, and it's assumed that you'll either use the [`--outFile`](/tsconfig#outFile) compiler option to join multiple input files into one output file, or use multiple `<script>` tags in your HTML to load these files (in the correct order!).
30
+
Inside a script file variables and types are declared to be in the shared global scope, and it's assumed that you'll either use the [`outFile`](/tsconfig#outFile) compiler option to join multiple input files into one output file, or use multiple `<script>` tags in your HTML to load these files (in the correct order!).
31
31
32
32
If you have a file that doesn't currently have any `import`s or `export`s, but you want to be treated as a module, add the line:
33
33
@@ -306,29 +306,29 @@ squareTwo;
306
306
307
307
### CommonJS and ES Modules interop
308
308
309
-
There is a mis-match in features between CommonJS and ES Module because ES Modules only support having the default export as an object, and never as a function. TypeScript has a compiler flag to reduce the friction between the two different sets of constraints with [`esModuleInterop`](/tsconfig/#esModuleInterop).
309
+
There is a mis-match in features between CommonJS and ES Module because ES Modules only support having the default export as an object, and never as a function. TypeScript has a compiler flag to reduce the friction between the two different sets of constraints with [`esModuleInterop`](/tsconfig#esModuleInterop).
310
310
311
311
## TypeScript's Module Resolution Options
312
312
313
313
Module resolution is the process of taking a string from the `import` or `require` statement, and determining what file that string refers to.
314
314
315
-
TypeScript includes two resolution strategies: Classic and Node. Classic, the default when the compiler flag[`module`](/tsconfig/#module) is not `commonjs`, is included for backwards compatibility.
315
+
TypeScript includes two resolution strategies: Classic and Node. Classic, the default when the compiler option[`module`](/tsconfig#module) is not `commonjs`, is included for backwards compatibility.
316
316
The Node strategy replicates how Node.js works in CommonJS mode, with additional checks for `.ts` and `.d.ts`.
317
317
318
-
There are many TSConfig flags which influence the module strategy within TypeScript: [`moduleResolution`](/tsconfig/#moduleResolution), [`baseUrl`](/tsconfig/#baseUrl), [`paths`](/tsconfig/#paths), [`rootDirs`](/tsconfig/#rootDirs).
318
+
There are many TSConfig flags which influence the module strategy within TypeScript: [`moduleResolution`](/tsconfig#moduleResolution), [`baseUrl`](/tsconfig#baseUrl), [`paths`](/tsconfig#paths), [`rootDirs`](/tsconfig#rootDirs).
319
319
320
320
For the full details on how these strategies work, you can consult the [Module Resolution](/docs/handbook/module-resolution.html).
321
321
322
322
## TypeScript's Module Output Options
323
323
324
324
There are two options which affect the emitted JavaScript output:
325
325
326
-
-[`target`](/tsconfig/#target) which determines which JS features are downleveled (converted to run in older JavaScript runtimes) and which are left intact
327
-
-[`module`](/tsconfig/#module) which determines what code is used for modules to interact with each other
326
+
-[`target`](/tsconfig#target) which determines which JS features are downleveled (converted to run in older JavaScript runtimes) and which are left intact
327
+
-[`module`](/tsconfig#module) which determines what code is used for modules to interact with each other
328
328
329
-
Which `target` you use is determined by the features available in the JavaScript runtime you expect to run the TypeScript code in. That could be: the oldest web browser you support, the lowest version of Node.js you expect to run on or could come from unique constraints from your runtime - like Electron for example.
329
+
Which [`target`](/tsconfig#target) you use is determined by the features available in the JavaScript runtime you expect to run the TypeScript code in. That could be: the oldest web browser you support, the lowest version of Node.js you expect to run on or could come from unique constraints from your runtime - like Electron for example.
330
330
331
-
All communication between modules happens via a module loader, the compiler flag[`module`](/tsconfig#module) determines which one is used.
331
+
All communication between modules happens via a module loader, the compiler option[`module`](/tsconfig#module) determines which one is used.
332
332
At runtime the module loader is responsible for locating and executing all dependencies of a module before executing it.
333
333
334
334
For example, here is a TypeScript file using ES Modules syntax, showcasing a few different options for [`module`](/tsconfig#module):
> Note that ES2020 is effectively the same as the original `index.ts`.
380
380
381
-
You can see all of the available options and what their emitted JavaScript code looks like in the [TSConfig Reference for `module`](/tsconfig/#module).
381
+
You can see all of the available options and what their emitted JavaScript code looks like in the [TSConfig Reference for `module`](/tsconfig#module).
0 commit comments