Skip to content

Commit 399c25f

Browse files
committed
Small book tweaks
1 parent 34c6541 commit 399c25f

File tree

4 files changed

+19
-22
lines changed

4 files changed

+19
-22
lines changed

book-content/chapters/08-classes.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ constructor(opts: { title: string; artist: string; releaseYear: number }) {
9898

9999
The `this` keyword refers to the instance of the class, and it's used to access the properties and methods of the class.
100100

101-
Now, when we create a new instance of the `Album` class, we can pass an object with the properties we want to set. If we don't provide any values, the default values will be used:
101+
Now, when we create a new instance of the `Album` class, we can pass an object with the properties we want to set.
102102

103103
```typescript
104104
const loopFindingJazzRecords = new Album({
@@ -108,10 +108,6 @@ const loopFindingJazzRecords = new Album({
108108
});
109109

110110
console.log(loopFindingJazzRecords.title); // Output: Loop Finding Jazz Records
111-
112-
const unknownAlbum = new Album();
113-
114-
console.log(unknownAlbum.title); // Output: Unknown Album
115111
```
116112

117113
### Using a Class as a Type

book-content/chapters/10-deriving-types.md

+11-10
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ Notice there is a lot of duplication here. Both the `FormValues` interface and `
520520

521521
Your task is to modify the `inputs` Record so its keys are derived from the `FormValues` interface.
522522

523-
<Exercise title="Exercise 1: Reduce Key Repetition" filePath="/src/040-deriving-types-from-values/125-keyof.problem.ts"></Exercise>
523+
<Exercise title="Exercise 1: Reduce Key Repetition" filePath="/src/040-deriving-types-from-values/125-keyof.problem.ts" resourceId="YgFRxBViy44CfW0H6uOLyz"></Exercise>
524524

525525
### Exercise 2: Derive a Type from a Value
526526

@@ -555,7 +555,7 @@ We want to use the `Environment` type across our application. However, the `conf
555555

556556
Your task is to update the `Environment` type so that it is derived from the `configurations` object.
557557

558-
<Exercise title="Exercise 2: Derive a Type from a Value" filePath="/src/040-deriving-types-from-values/126-typeof-keyword.problem.ts"></Exercise>
558+
<Exercise title="Exercise 2: Derive a Type from a Value" filePath="/src/040-deriving-types-from-values/126-typeof-keyword.problem.ts" resourceId="YgFRxBViy44CfW0H6uOMPr"></Exercise>
559559

560560
### Exercise 3: Accessing Specific Values
561561

@@ -587,7 +587,7 @@ type test = Expect<Equal<Group, "group">>;
587587

588588
Your task is to find the proper way to type `Group` so the test passes as expected.
589589

590-
<Exercise title="Exercise 3: Accessing Specific Values" filePath="/src/040-deriving-types-from-values/135-indexed-access-types.problem.ts"></Exercise>
590+
<Exercise title="Exercise 3: Accessing Specific Values" filePath="/src/040-deriving-types-from-values/135-indexed-access-types.problem.ts" resourceId="5EnWog8KD1gKEzaFObJmOY"></Exercise>
591591

592592
### Exercise 4: Unions with Indexed Access Types
593593

@@ -617,7 +617,7 @@ type test = Expect<
617617

618618
This time, your challenge is to update the `PlannedPrograms` type to use an indexed access type to extract a union of the `ProgramModeMap` values that included "`planned`".
619619

620-
<Exercise title="Exercise 4: Unions with Indexed Access Types" filePath="/src/040-deriving-types-from-values/136-pass-unions-to-indexed-access-types.problem.ts"></Exercise>
620+
<Exercise title="Exercise 4: Unions with Indexed Access Types" filePath="/src/040-deriving-types-from-values/136-pass-unions-to-indexed-access-types.problem.ts" resourceId="5EnWog8KD1gKEzaFObJmhp"></Exercise>
621621

622622
### Exercise 5: Extract a Union of All Values
623623

@@ -658,7 +658,7 @@ type test = Expect<
658658

659659
Using what you've learned so far, your task is to update the `AllPrograms` type to use an indexed access type to create a union of all the values from the `programModeEnumMap` object.
660660

661-
<Exercise title="Exercise 5: Extract a Union of All Values" filePath="/src/040-deriving-types-from-values/137-pass-keyof-into-an-indexed-access-type.problem.ts"></Exercise>
661+
<Exercise title="Exercise 5: Extract a Union of All Values" filePath="/src/040-deriving-types-from-values/137-pass-keyof-into-an-indexed-access-type.problem.ts" resourceId="VtQChjOYAJCkX9MVx78MCb"></Exercise>
662662

663663
### Exercise 6: Create a Union from an `as const` Array
664664

@@ -681,6 +681,7 @@ A test has been written to check if an `AllPrograms` type is a union of all the
681681
import { Equal, Expect } from "@total-typescript/helpers";
682682
type AllPrograms = unknown;
683683
// ---cut---
684+
684685
type test = Expect<
685686
Equal<
686687
AllPrograms,
@@ -698,7 +699,7 @@ Your task is to determine how to create the `AllPrograms` type in order for the
698699

699700
Note that just using `keyof` and `typeof` in an approach similar to the previous exercise's solution won't quite work to solve this one! This is tricky to find - but as a hint: you can pass primitive types to indexed access types.
700701

701-
<Exercise title="Exercise 6: Create a Union from an `as const` Array" filePath="/src/040-deriving-types-from-values/138-create-a-union-from-an-as-const-array.problem.ts"></Exercise>
702+
<Exercise title="Exercise 6: Create a Union from an `as const` Array" filePath="/src/040-deriving-types-from-values/138-create-a-union-from-an-as-const-array.problem.ts" resourceId="AhnoaCs5v1qlRT7GjJoi5Y"></Exercise>
702703

703704
### Solution 1: Reduce Key Repetition
704705

@@ -1050,7 +1051,7 @@ In addition to being a bit annoying to write and read, the other problem with th
10501051
10511052
Your task is to use a utility type to fix this problem.
10521053
1053-
<Exercise title="Exercise 7: A Single Source of Truth" filePath="/src/040-deriving-types-from-values/132-parameters-type-helper.problem.ts"></Exercise>
1054+
<Exercise title="Exercise 7: A Single Source of Truth" filePath="/src/040-deriving-types-from-values/132-parameters-type-helper.problem.ts" resourceId="YgFRxBViy44CfW0H6uOO1f"></Exercise>
10541055
10551056
### Exercise 8: Typing Based on Return Value
10561057
@@ -1090,7 +1091,7 @@ type test = Expect<
10901091
10911092
Your task is to update the `User` type so the test passes as expected.
10921093
1093-
<Exercise title="Exercise 8: Typing Based on Return Value" filePath="/src/040-deriving-types-from-values/133-return-type.problem.ts"></Exercise>
1094+
<Exercise title="Exercise 8: Typing Based on Return Value" filePath="/src/040-deriving-types-from-values/133-return-type.problem.ts" resourceId="YgFRxBViy44CfW0H6uOPRx"></Exercise>
10941095
10951096
### Exercise 9: Unwrapping a Promise
10961097
@@ -1124,7 +1125,7 @@ Like before, assume that you do not have access to the implementation of the `fe
11241125
11251126
Your task is to update the `User` type so the test passes as expected.
11261127
1127-
<Exercise title="Exercise 9: Unwrapping a Promise" filePath="/src/040-deriving-types-from-values/134-awaited-type-helper.problem.ts"></Exercise>
1128+
<Exercise title="Exercise 9: Unwrapping a Promise" filePath="/src/040-deriving-types-from-values/134-awaited-type-helper.problem.ts" resourceId="AhnoaCs5v1qlRT7GjJofuY"></Exercise>
11281129
11291130
### Solution 7: A Single Source of Truth
11301131
@@ -1340,7 +1341,7 @@ Similarly to `Exclude`, `Extract` works by pattern matching. It will extract any
13401341
13411342
This means that, to reverse our `Extract` example earlier, we can use it to extract all strings from a union:
13421343
1343-
```typescript
1344+
```ts twoslash
13441345
type Example = "a" | "b" | 1 | 2 | true | false;
13451346

13461347
type Strings = Extract<Example, string>;

book-content/chapters/14-configuring-typescript.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ This code will error in your target environment, because `target` won't transfor
141141

142142
If you're not sure what to specify for `target`, keep it up to date with the version you have specified in `lib`.
143143

144-
### `esModuleInterop`
144+
### `esModuleInterop`
145145

146146
`esModuleInterop` is an old flag, released in 2018. It helps with interoperability between CommonJS and ES modules. At the time, TypeScript had deviated slightly from commonly-used tools like Babel in how it handled wildcard imports and default exports. `esModuleInterop` brought TypeScript in line with these tools.
147147

@@ -164,7 +164,7 @@ declare const enum AlbumFormat {
164164
Digital,
165165
}
166166

167-
const largestPhysicalSize = AlbumFormat.Vinyl; // red squiggly line under AlbumFormat when isolatedModules is enabled
167+
const largestPhysicalSize = AlbumFormat.Vinyl;
168168
```
169169

170170
Recall that the `declare` keyword will place `const enum` in an ambient context, which means that it would be erased at runtime.
@@ -241,7 +241,7 @@ const nonExistentTrack = egoMirror.tracks[3];
241241
console.log(nonExistentTrack.toUpperCase()); // no error in VS Code
242242

243243
// However, running the code results in a runtime error:
244-
TypeError: Cannot read property 'toUpperCase' of undefined
244+
// TypeError: Cannot read property 'toUpperCase' of undefined
245245
```
246246

247247
By setting `noUncheckedIndexedAccess` to `true`, TypeScript will infer the type of every indexed access to be `T | undefined` instead of just `T`. In this case, every entry in `egoMirror.tracks` would be of type `string | undefined`:
@@ -286,7 +286,7 @@ const egoMirror: VinylSingle = {
286286

287287
const ego = egoMirror.tracks[0];
288288
// ---cut---
289-
console.log(ego.toUpperCase()); // red squiggly line under ego
289+
console.log(ego.toUpperCase());
290290
```
291291

292292
This means that we have to handle the possibility of `undefined` values when accessing array or object indices.

book-content/chapters/16-the-utils-folder.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const identity = <T>(arg: T): T => arg;
5151
We can even declare a generic function as a type:
5252

5353
```typescript
54-
type Identity = <T>(arg: T) => void;
54+
type Identity = <T>(arg: T) => T;
5555

5656
const identity: Identity = (arg) => arg;
5757
```
@@ -68,12 +68,12 @@ It's very important not to confuse the syntax for a generic type with the syntax
6868

6969
```typescript
7070
// Type alias for a generic function
71-
type Identity = <T>(arg: T) => void;
71+
type Identity = <T>(arg: T) => T;
7272
// ^^^
7373
// Type parameter belongs to the function
7474

7575
// Generic type
76-
type Identity<T> = (arg: T) => void;
76+
type Identity<T> = (arg: T) => T;
7777
// ^^^
7878
// Type parameter belongs to the type
7979
```

0 commit comments

Comments
 (0)