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: book-content/chapters/13-modules-scripts-declaration-files.md
+18-19Lines changed: 18 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,9 +16,10 @@ const DEFAULT_VOLUME = 90;
16
16
17
17
Without being imported, the `DEFAULT_VOLUME` constant is not accessible from other files:
18
18
19
-
```typescript
19
+
```ts twoslash
20
+
// @errors: 2304
20
21
// inside of index.ts
21
-
console.log(DEFAULT_VOLUME);// red squiggly line under DEFAULT_VOLUME
22
+
console.log(DEFAULT_VOLUME);
22
23
```
23
24
24
25
In order to use the `DEFAULT_VOLUME` constant in the `index.ts` file, it must be imported from the `constants.ts` module:
@@ -45,10 +46,13 @@ const DEFAULT_VOLUME = 90;
45
46
46
47
Now, we no longer need to import the `DEFAULT_VOLUME` constant in the `index.ts` file:
47
48
48
-
```typescript
49
+
```ts twoslash
50
+
declareconst DEFAULT_VOLUME:90;
51
+
// ---cut---
49
52
// inside of index.ts
50
53
51
-
console.log(DEFAULT_VOLUME); // 90
54
+
console.log(DEFAULT_VOLUME);
55
+
// ^?
52
56
```
53
57
54
58
This behavior might be surprising to you - let's figure out why TypeScript does this.
@@ -77,11 +81,10 @@ But these days, 99% of the code you'll be writing will be in modules. So this au
77
81
78
82
Let's imagine you create a new TypeScript file, `utils.ts`, and add a `name` constant:
79
83
80
-
```typescript
81
-
const name ="Alice"; // red squiggly line under name
82
-
83
-
// Hovering over the error shows:
84
-
// Cannot redeclare block-scoped variable 'name'.
84
+
```ts twoslash
85
+
// @errors: 2451
86
+
// @moduleDetection: auto
87
+
const name ="Alice";
85
88
```
86
89
87
90
You'll be greeted with a surprising error. This error is telling you that you can't declare `name`, because it's already been declared.
@@ -228,9 +231,7 @@ Now, the `Album` type is available globally and can be used in any TypeScript fi
228
231
229
232
What would happen if we tried to write normal TypeScript inside our `.d.ts` file?
230
233
231
-
```tsx
232
-
// musicPlayer.d.ts
233
-
234
+
```ts
234
235
exportfunction playTrack(track: {
235
236
title:string;
236
237
artist:string;
@@ -442,9 +443,9 @@ declare module "express" {
442
443
443
444
Now, we've completely overridden the `express` module. This means that the `express` module no longer has any exports except for `MyType`:
444
445
445
-
```typescript
446
+
```ts
446
447
// anywhere.ts
447
-
import { express } from"express"; // red squiggly line under "express"
448
+
import { Express } from"express"; // red squiggly line under "Express"
448
449
```
449
450
450
451
Just like module augmentation, we can get the same behavior by changing `express.d.ts` to `express.ts` (if `moduleDetection` is set to `auto`).
@@ -499,8 +500,6 @@ Looking at the code in `node_modules/typescript/lib`, you'll see dozens of decla
499
500
500
501
Understanding how to navigate these declaration files can be very useful for fixing type errors. Take a few minutes to explore what's in `lib.es5.d.ts` by using 'go to definition' to navigate around.
501
502
502
-
<!-- TODO - add a section on the most common global types -->
503
-
504
503
#### Choosing Your JavaScript Version With `lib`
505
504
506
505
The `lib` setting in `tsconfig.json` lets you choose which `.d.ts` files are included in your project. Choosing `es2022` will give you all the JavaScript features up to ES2022. Choosing `es5` will give you all the features up to ES5.
TypeScript reports an error underneath the `import` statement because it can't find type definitions, even though the library is installed over 40 million times a week from NPM:
617
616
618
-
```tsx
619
-
//hovering over "diff" shows:
620
-
//Could not find a declaration file for module 'diff'. Try `npm install --save-dev @types/diff` if it exists or add a new declaration (.d.ts) file containing `declare module 'diff';`
617
+
```txt
618
+
hovering over "diff" shows:
619
+
Could not find a declaration file for module 'diff'. Try `npm install --save-dev @types/diff` if it exists or add a new declaration (.d.ts) file containing `declare module 'diff';`
621
620
```
622
621
623
622
Since we're using `pnpm` instead of `npm`, our installation command looks like this:
0 commit comments