Skip to content

Commit b4e5ce4

Browse files
committed
More twoslashing
1 parent 4d6b6ff commit b4e5ce4

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

book-content/chapters/13-modules-scripts-declaration-files.md

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ const DEFAULT_VOLUME = 90;
1616

1717
Without being imported, the `DEFAULT_VOLUME` constant is not accessible from other files:
1818

19-
```typescript
19+
```ts twoslash
20+
// @errors: 2304
2021
// inside of index.ts
21-
console.log(DEFAULT_VOLUME); // red squiggly line under DEFAULT_VOLUME
22+
console.log(DEFAULT_VOLUME);
2223
```
2324

2425
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;
4546

4647
Now, we no longer need to import the `DEFAULT_VOLUME` constant in the `index.ts` file:
4748

48-
```typescript
49+
```ts twoslash
50+
declare const DEFAULT_VOLUME: 90;
51+
// ---cut---
4952
// inside of index.ts
5053

51-
console.log(DEFAULT_VOLUME); // 90
54+
console.log(DEFAULT_VOLUME);
55+
// ^?
5256
```
5357

5458
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
7781

7882
Let's imagine you create a new TypeScript file, `utils.ts`, and add a `name` constant:
7983

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";
8588
```
8689

8790
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
228231

229232
What would happen if we tried to write normal TypeScript inside our `.d.ts` file?
230233

231-
```tsx
232-
// musicPlayer.d.ts
233-
234+
```ts
234235
export function playTrack(track: {
235236
title: string;
236237
artist: string;
@@ -442,9 +443,9 @@ declare module "express" {
442443

443444
Now, we've completely overridden the `express` module. This means that the `express` module no longer has any exports except for `MyType`:
444445

445-
```typescript
446+
```ts
446447
// anywhere.ts
447-
import { express } from "express"; // red squiggly line under "express"
448+
import { Express } from "express"; // red squiggly line under "Express"
448449
```
449450

450451
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
499500

500501
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.
501502

502-
<!-- TODO - add a section on the most common global types -->
503-
504503
#### Choosing Your JavaScript Version With `lib`
505504

506505
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.
@@ -615,9 +614,9 @@ const differences = Diff.diffChars(message1, message2);
615614

616615
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:
617616

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';`
621620
```
622621

623622
Since we're using `pnpm` instead of `npm`, our installation command looks like this:

0 commit comments

Comments
 (0)