Skip to content

Commit

Permalink
Small book tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Jul 10, 2024
1 parent 34c6541 commit 399c25f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
6 changes: 1 addition & 5 deletions book-content/chapters/08-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ constructor(opts: { title: string; artist: string; releaseYear: number }) {

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

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:
Now, when we create a new instance of the `Album` class, we can pass an object with the properties we want to set.

```typescript
const loopFindingJazzRecords = new Album({
Expand All @@ -108,10 +108,6 @@ const loopFindingJazzRecords = new Album({
});

console.log(loopFindingJazzRecords.title); // Output: Loop Finding Jazz Records

const unknownAlbum = new Album();

console.log(unknownAlbum.title); // Output: Unknown Album
```

### Using a Class as a Type
Expand Down
21 changes: 11 additions & 10 deletions book-content/chapters/10-deriving-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ Notice there is a lot of duplication here. Both the `FormValues` interface and `

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

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

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

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

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

<Exercise title="Exercise 2: Derive a Type from a Value" filePath="/src/040-deriving-types-from-values/126-typeof-keyword.problem.ts"></Exercise>
<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>

### Exercise 3: Accessing Specific Values

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

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

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

### Exercise 4: Unions with Indexed Access Types

Expand Down Expand Up @@ -617,7 +617,7 @@ type test = Expect<

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`".

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

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

Expand Down Expand Up @@ -658,7 +658,7 @@ type test = Expect<

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.

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

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

Expand All @@ -681,6 +681,7 @@ A test has been written to check if an `AllPrograms` type is a union of all the
import { Equal, Expect } from "@total-typescript/helpers";
type AllPrograms = unknown;
// ---cut---

type test = Expect<
Equal<
AllPrograms,
Expand All @@ -698,7 +699,7 @@ Your task is to determine how to create the `AllPrograms` type in order for the

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.

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

### Solution 1: Reduce Key Repetition

Expand Down Expand Up @@ -1050,7 +1051,7 @@ In addition to being a bit annoying to write and read, the other problem with th
Your task is to use a utility type to fix this problem.
<Exercise title="Exercise 7: A Single Source of Truth" filePath="/src/040-deriving-types-from-values/132-parameters-type-helper.problem.ts"></Exercise>
<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>
### Exercise 8: Typing Based on Return Value
Expand Down Expand Up @@ -1090,7 +1091,7 @@ type test = Expect<
Your task is to update the `User` type so the test passes as expected.
<Exercise title="Exercise 8: Typing Based on Return Value" filePath="/src/040-deriving-types-from-values/133-return-type.problem.ts"></Exercise>
<Exercise title="Exercise 8: Typing Based on Return Value" filePath="/src/040-deriving-types-from-values/133-return-type.problem.ts" resourceId="YgFRxBViy44CfW0H6uOPRx"></Exercise>
### Exercise 9: Unwrapping a Promise
Expand Down Expand Up @@ -1124,7 +1125,7 @@ Like before, assume that you do not have access to the implementation of the `fe
Your task is to update the `User` type so the test passes as expected.
<Exercise title="Exercise 9: Unwrapping a Promise" filePath="/src/040-deriving-types-from-values/134-awaited-type-helper.problem.ts"></Exercise>
<Exercise title="Exercise 9: Unwrapping a Promise" filePath="/src/040-deriving-types-from-values/134-awaited-type-helper.problem.ts" resourceId="AhnoaCs5v1qlRT7GjJofuY"></Exercise>
### Solution 7: A Single Source of Truth
Expand Down Expand Up @@ -1340,7 +1341,7 @@ Similarly to `Exclude`, `Extract` works by pattern matching. It will extract any
This means that, to reverse our `Extract` example earlier, we can use it to extract all strings from a union:
```typescript
```ts twoslash
type Example = "a" | "b" | 1 | 2 | true | false;

type Strings = Extract<Example, string>;
Expand Down
8 changes: 4 additions & 4 deletions book-content/chapters/14-configuring-typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ This code will error in your target environment, because `target` won't transfor

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

### `esModuleInterop`
### `esModuleInterop`

`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.

Expand All @@ -164,7 +164,7 @@ declare const enum AlbumFormat {
Digital,
}

const largestPhysicalSize = AlbumFormat.Vinyl; // red squiggly line under AlbumFormat when isolatedModules is enabled
const largestPhysicalSize = AlbumFormat.Vinyl;
```

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

// However, running the code results in a runtime error:
TypeError: Cannot read property 'toUpperCase' of undefined
// TypeError: Cannot read property 'toUpperCase' of undefined
```

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`:
Expand Down Expand Up @@ -286,7 +286,7 @@ const egoMirror: VinylSingle = {

const ego = egoMirror.tracks[0];
// ---cut---
console.log(ego.toUpperCase()); // red squiggly line under ego
console.log(ego.toUpperCase());
```

This means that we have to handle the possibility of `undefined` values when accessing array or object indices.
Expand Down
6 changes: 3 additions & 3 deletions book-content/chapters/16-the-utils-folder.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const identity = <T>(arg: T): T => arg;
We can even declare a generic function as a type:

```typescript
type Identity = <T>(arg: T) => void;
type Identity = <T>(arg: T) => T;

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

```typescript
// Type alias for a generic function
type Identity = <T>(arg: T) => void;
type Identity = <T>(arg: T) => T;
// ^^^
// Type parameter belongs to the function

// Generic type
type Identity<T> = (arg: T) => void;
type Identity<T> = (arg: T) => T;
// ^^^
// Type parameter belongs to the type
```
Expand Down

0 comments on commit 399c25f

Please sign in to comment.