Skip to content

Commit 20e4e5b

Browse files
committed
More twoslashing
1 parent 369f7c2 commit 20e4e5b

File tree

4 files changed

+571
-305
lines changed

4 files changed

+571
-305
lines changed

book-content/chapters/07-mutability.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ When using `const`, the variable is _immutable_ and cannot be reassigned. When w
7777
type AlbumGenre = "rock" | "country" | "electronic";
7878

7979
// ---cut---
80-
8180
const albumGenre = "rock";
8281

8382
const handleGenre = (genre: AlbumGenre) => {

book-content/chapters/09-typescript-only-features.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,26 @@ logStatus(0);
230230

231231
If we call it with a number that isn't a member of the enum, TypeScript will report an error:
232232

233-
```typescript
234-
logStatus(3); // Argument of type '3' is not assignable to parameter of type 'AlbumStatus'.
233+
```ts twoslash
234+
// @errors: 2345
235+
enum AlbumStatus {
236+
NewRelease = 0,
237+
OnSale = 1,
238+
StaffPick = 2,
239+
}
240+
241+
function logStatus(genre: AlbumStatus) {
242+
console.log(genre);
243+
}
244+
245+
// ---cut---
246+
logStatus(3);
235247
```
236248

237249
This is different from string enums, which only allow the enum members to be used as types:
238250

239-
```typescript
251+
```ts twoslash
252+
// @errors: 2345
240253
enum AlbumStatus {
241254
NewRelease = "NEW_RELEASE",
242255
OnSale = "ON_SALE",
@@ -248,7 +261,7 @@ function logStatus(genre: AlbumStatus) {
248261
}
249262

250263
logStatus(AlbumStatus.NewRelease);
251-
logStatus("NEW_RELEASE"); // Argument of type '"NEW_RELEASE"' is not assignable to parameter of type 'AlbumStatus'.
264+
logStatus("NEW_RELEASE");
252265
```
253266

254267
The way string enums behave feels more natural - it matches how enums work in other languages like C# and Java.
@@ -259,14 +272,25 @@ In fact, string enums are unique in TypeScript because they're compared _nominal
259272

260273
This means that two string enums with the same members are considered different types if they have different names:
261274

262-
```typescript
275+
```ts twoslash
276+
// @errors: 2345
277+
enum AlbumStatus {
278+
NewRelease = "NEW_RELEASE",
279+
OnSale = "ON_SALE",
280+
StaffPick = "STAFF_PICK",
281+
}
282+
function logStatus(genre: AlbumStatus) {
283+
console.log(genre);
284+
}
285+
286+
// ---cut---
263287
enum AlbumStatus2 {
264288
NewRelease = "NEW_RELEASE",
265289
OnSale = "ON_SALE",
266290
StaffPick = "STAFF_PICK",
267291
}
268292

269-
logStatus(AlbumStatus2.NewRelease); // Argument of type 'AlbumStatus2' is not assignable to parameter of type 'AlbumStatus'.
293+
logStatus(AlbumStatus2.NewRelease);
270294
```
271295

272296
For those of us used to structural typing, this can be a bit of a surprise. But to developers used to enums in other languages, string enums will feel the most natural.

0 commit comments

Comments
 (0)