Skip to content

Commit 5f92659

Browse files
committed
Fixed bad cuts
1 parent 47141d2 commit 5f92659

9 files changed

+25
-47
lines changed

book-content/chapters/04-essential-types-and-annotations.md

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ But this one would show an error:
9090
// @errors: 2322
9191
let isReleased: boolean = true;
9292

93-
isReleased = "yes"; // red squiggly line under `isReleased`
93+
isReleased = "yes";
9494
```
9595

9696
TypeScript's static type checking is able to spot errors at compile time, which is happening behind the scenes as you write your code.
@@ -259,7 +259,6 @@ export const add = (a: boolean, b: boolean) => {
259259
};
260260

261261
// ---cut---
262-
263262
const result = add(1, 2);
264263

265264
type test = Expect<Equal<typeof result, number>>;
@@ -283,7 +282,7 @@ Finally, we can see that our `test` result has an error because the `result` is
283282

284283
Your challenge is to consider how we can change the types to make the errors go away, and to ensure that `result` is a `number`. You can hover over `result` to check it.
285284

286-
<Exercise title="Exercise 1: Basic Types with Function Parameters" filePath="/src/015-essential-types-and-annotations/020-basic-types-with-function-parameters.problem.ts"></Exercise>
285+
<Exercise title="Exercise 1: Basic Types with Function Parameters" filePath="/src/015-essential-types-and-annotations/020-basic-types-with-function-parameters.problem.ts" resourceId="1fZdJK1AI9JNeRElmqAd9N"></Exercise>
287286

288287
#### Exercise 2: Annotating Empty Parameters
289288

@@ -309,15 +308,14 @@ const concatTwoStrings = (a, b) => {
309308
};
310309

311310
// ---cut---
312-
313311
const result = concatTwoStrings("Hello", "World");
314312

315313
type test = Expect<Equal<typeof result, string>>;
316314
```
317315

318316
Your job is to add some function paramater annotations to the `concatTwoStrings` function to make the errors go away.
319317

320-
<Exercise title="Exercise 2: Annotating Empty Parameters" filePath="/src/015-essential-types-and-annotations/021-annotating-empty-parameters.problem.ts"></Exercise>
318+
<Exercise title="Exercise 2: Annotating Empty Parameters" filePath="/src/015-essential-types-and-annotations/021-annotating-empty-parameters.problem.ts" resourceId="1fZdJK1AI9JNeRElmqAdKe"></Exercise>
321319

322320
#### Exercise 3: The Basic Types
323321

@@ -354,13 +352,13 @@ Type 'boolean' is not assignable to type 'string'.
354352

355353
Change the types of the annotations on each variable to make the errors go away.
356354

357-
<Exercise title="Exercise 3: The Basic Types" filePath="/src/015-essential-types-and-annotations/022-all-types.problem.ts"></Exercise>
355+
<Exercise title="Exercise 3: The Basic Types" filePath="/src/015-essential-types-and-annotations/022-all-types.problem.ts" resourceId="NMpTvrI4rUCyVa4GVzY1iN"></Exercise>
358356

359357
#### Exercise 4: The `any` Type
360358

361359
Here is a function called `handleFormData` that accepts an `e` typed as `any`. The function prevents the default form submission behavior, then creates an object from the form data and returns it:
362360

363-
```ts twoslash
361+
```ts
364362
const handleFormData = (e: any) => {
365363
e.preventDefault();
366364

@@ -414,7 +412,7 @@ Why is this error happening? Why isn't TypeScript giving us an error here?
414412

415413
I'll give you a clue. I've hidden a nasty typo in there. Can you fix it?
416414

417-
<Exercise title="Exercise 4: The `any` Type" filePath="/src/015-essential-types-and-annotations/032.5-any.problem.ts"></Exercise>
415+
<Exercise title="Exercise 4: The `any` Type" filePath="/src/015-essential-types-and-annotations/032.5-any.problem.ts" resourceId="1fZdJK1AI9JNeRElmqAeU3"></Exercise>
418416

419417
#### Solution 1: Basic Types with Function Parameters
420418

@@ -518,7 +516,7 @@ Using `any` also disables useful features like autocompletion, which can help yo
518516

519517
That's right-- the error in the above code was caused by a typo of `e.terget` instead of `e.target` when creating the `FormData`!
520518

521-
```ts twoslash
519+
```ts
522520
const handleFormData = (e: any) => {
523521
e.preventDefault();
524522

@@ -604,7 +602,7 @@ We can tell from the `concatName` function body that it expects `user.first` and
604602

605603
How could we type the `user` parameter to ensure that it has these properties and that they are of the correct type?
606604

607-
<Exercise title="Exercise 1: Object Literal Types" filePath="/src/015-essential-types-and-annotations/025-object-literal-types.problem.ts"></Exercise>
605+
<Exercise title="Exercise 1: Object Literal Types" filePath="/src/015-essential-types-and-annotations/025-object-literal-types.problem.ts" resourceId="1fZdJK1AI9JNeRElmqAdzz"></Exercise>
608606

609607
#### Exercise 2: Optional Property Types
610608

@@ -639,7 +637,7 @@ const concatName = (user: { first: string; last: string }) => {
639637
it("should only return the first name if last name not provided", () => {
640638
const result = concatName({
641639
first: "John",
642-
}); // red squiggly line under `{first: "John"}`
640+
});
643641

644642
type test = Expect<Equal<typeof result, string>>;
645643

@@ -653,7 +651,7 @@ The error tells us that we are missing a property, but the error is incorrect. W
653651

654652
How would you update this function to fix the errors?
655653

656-
<Exercise title="Exercise 2: Optional Property Types" filePath="/src/015-essential-types-and-annotations/026-optional-property-types.problem.ts"></Exercise>
654+
<Exercise title="Exercise 2: Optional Property Types" filePath="/src/015-essential-types-and-annotations/026-optional-property-types.problem.ts" resourceId="1fZdJK1AI9JNeRElmqAeIm"></Exercise>
657655

658656
#### Solution 1: Object Literal Types
659657

@@ -839,7 +837,7 @@ Even though everything is working as expected, there's an opportunity for refact
839837

840838
How could you use the `type` keyword to make this code more readable?
841839

842-
<Exercise title="Exercise 1: The `type` Keyword" filePath="/src/015-essential-types-and-annotations/027-type-keyword.problem.ts"></Exercise>
840+
<Exercise title="Exercise 1: The `type` Keyword" filePath="/src/015-essential-types-and-annotations/027-type-keyword.problem.ts" resourceId="jUJqrXCHRph0Z4Fs6Ll3za"></Exercise>
843841

844842
#### Solution 1: The `type` Keyword
845843

@@ -1051,7 +1049,7 @@ As the error message points out, there is not currently a property called `items
10511049

10521050
How would you fix this error?
10531051

1054-
<Exercise title="Exercise 1: Array Type" filePath="/src/015-essential-types-and-annotations/028-arrays.problem.ts"></Exercise>
1052+
<Exercise title="Exercise 1: Array Type" filePath="/src/015-essential-types-and-annotations/028-arrays.problem.ts" resourceId="jUJqrXCHRph0Z4Fs6Ll3za"></Exercise>
10551053

10561054
#### Exercise 2: Arrays of Objects
10571055

@@ -1088,7 +1086,7 @@ Object literal may only specify known properties, and 'ingredients' does not exi
10881086

10891087
By combining what you've seen with typing object properties and working with arrays, how would you specify ingredients for the `Recipe` type?
10901088

1091-
<Exercise title="Exercise 2: Arrays of Objects" filePath="/src/015-essential-types-and-annotations/029-arrays-of-objects.problem.ts"></Exercise>
1089+
<Exercise title="Exercise 2: Arrays of Objects" filePath="/src/015-essential-types-and-annotations/029-arrays-of-objects.problem.ts" resourceId="YgFRxBViy44CfW0H2dToDx"></Exercise>
10921090

10931091
#### Exercise 3: Tuples
10941092

@@ -1143,7 +1141,7 @@ setRange([0, 10, 20]);
11431141

11441142
The code for the `setRange` function needs an updated type annotation to specify that it only accepts a tuple of two numbers.
11451143

1146-
<Exercise title="Exercise 3: Tuples" filePath="/src/015-essential-types-and-annotations/031-tuples.problem.ts"></Exercise>
1144+
<Exercise title="Exercise 3: Tuples" filePath="/src/015-essential-types-and-annotations/031-tuples.problem.ts" resourceId="YgFRxBViy44CfW0H2dTomV"></Exercise>
11471145

11481146
#### Exercise 4: Optional Members of Tuples
11491147

@@ -1162,16 +1160,16 @@ const goToLocation = (coordinates: Array<number>) => {
11621160
// Do something with latitude, longitude, and elevation in here
11631161

11641162
type tests = [
1165-
Expect<Equal<typeof latitude, number>>, // red squiggly line under Equal<> statement
1166-
Expect<Equal<typeof longitude, number>>, // red squiggly line under Equal<> statement
1163+
Expect<Equal<typeof latitude, number>>,
1164+
Expect<Equal<typeof longitude, number>>,
11671165
Expect<Equal<typeof elevation, number | undefined>>,
11681166
];
11691167
};
11701168
```
11711169

11721170
Your challenge is to update the type annotation for the `coordinates` parameter to specify that it should be a tuple of three numbers, where the third number is optional.
11731171

1174-
<Exercise title="Exercise 4: Optional Members of Tuples" filePath="/src/015-essential-types-and-annotations/032-optional-members-of-tuples.problem.ts"></Exercise>
1172+
<Exercise title="Exercise 4: Optional Members of Tuples" filePath="/src/015-essential-types-and-annotations/032-optional-members-of-tuples.problem.ts" resourceId="jUJqrXCHRph0Z4Fs6Ll7aP"></Exercise>
11751173

11761174
#### Solution 1: Array Type
11771175

@@ -1453,7 +1451,7 @@ new () => Map<any, any> (+3 overloads)
14531451

14541452
How would we type the `userMap` so the key must be a number and the value is an object with `name` and `age` properties?
14551453

1456-
<Exercise title="Exercise 1: Passing Types to Map" filePath="/src/015-essential-types-and-annotations/036-pass-types-to-map.problem.ts"></Exercise>
1454+
<Exercise title="Exercise 1: Passing Types to Map" filePath="/src/015-essential-types-and-annotations/036-pass-types-to-map.problem.ts" resourceId="YgFRxBViy44CfW0H2dTq1H"></Exercise>
14571455

14581456
#### Exercise 2: `JSON.parse()` Can't Receive Type Arguments
14591457

@@ -1484,7 +1482,6 @@ const parsedData = JSON.parse<{
14841482
// ---cut---
14851483
type test = Expect<
14861484
Equal<
1487-
// red squiggly lines for the full Equal<>
14881485
typeof parsedData,
14891486
{
14901487
name: string;
@@ -1507,7 +1504,7 @@ The test errors tell us that the type of `parsed` is not what we expect. The pro
15071504

15081505
Why this is happening? What would be an different way to correct these type errors?
15091506

1510-
<Exercise title="Exercise 2: `JSON.parse()` Can't Receive Type Arguments" filePath="/src/015-essential-types-and-annotations/037-json-parse-cant-receive-type-arguments.problem.ts"></Exercise>
1507+
<Exercise title="Exercise 2: `JSON.parse()` Can't Receive Type Arguments" filePath="/src/015-essential-types-and-annotations/037-json-parse-cant-receive-type-arguments.problem.ts" resourceId="1fZdJK1AI9JNeRElmqAfD9"></Exercise>
15111508

15121509
#### Solution 1: Passing Types to Map
15131510

@@ -1866,13 +1863,12 @@ const concatName = (first: string, last: string) => {
18661863
return `${first} ${last}`;
18671864
};
18681865
// ---cut---
1869-
18701866
const result2 = concatName("John");
18711867
```
18721868

18731869
Try to use an optional parameter annotation to fix the error.
18741870

1875-
<Exercise title="Exercise 1: Optional Function Parameters" filePath="/src/015-essential-types-and-annotations/023-optional-function-parameters.problem.ts"></Exercise>
1871+
<Exercise title="Exercise 1: Optional Function Parameters" filePath="/src/015-essential-types-and-annotations/023-optional-function-parameters.problem.ts" resourceId="1fZdJK1AI9JNeRElmqAdVv"></Exercise>
18761872

18771873
#### Exercise 2: Default Function Parameters
18781874

@@ -1930,7 +1926,7 @@ expect(result).toEqual("John Pocock");
19301926

19311927
Update the `concatName` function to use `Pocock` as the default last name if one is not provided.
19321928

1933-
<Exercise title="Exercise 2: Default Function Parameters" filePath="/src/015-essential-types-and-annotations/024-default-function-parameters.problem.ts"></Exercise>
1929+
<Exercise title="Exercise 2: Default Function Parameters" filePath="/src/015-essential-types-and-annotations/024-default-function-parameters.problem.ts" resourceId="1fZdJK1AI9JNeRElmqAdoi"></Exercise>
19341930

19351931
#### Exercise 3: Rest Parameters
19361932

@@ -1959,7 +1955,7 @@ The test passes, but there's an error on the `...strings` rest parameter.
19591955

19601956
How would you update the rest parameter to specify that it should be an array of strings?
19611957

1962-
<Exercise title="Exercise 3: Rest Parameters" filePath="/src/015-essential-types-and-annotations/030-rest-parameters.problem.ts"></Exercise>
1958+
<Exercise title="Exercise 3: Rest Parameters" filePath="/src/015-essential-types-and-annotations/030-rest-parameters.problem.ts" resourceId="jUJqrXCHRph0Z4Fs6Ll6T5"></Exercise>
19631959

19641960
#### Exercise 4: Function Types
19651961

@@ -2011,7 +2007,6 @@ const users: User[] = [
20112007
];
20122008

20132009
modifyUser(users, "1", (user) => {
2014-
// red squiggly line under `user`
20152010
return { ...user, name: "Waqas" };
20162011
});
20172012
```
@@ -2033,7 +2028,7 @@ modifyUser(
20332028

20342029
How would you type `makeChange` as a function takes in a `User` and returns a `User`?
20352030

2036-
<Exercise title="Exercise 4: Function Types" filePath="/src/015-essential-types-and-annotations/033-function-types.problem.ts"></Exercise>
2031+
<Exercise title="Exercise 4: Function Types" filePath="/src/015-essential-types-and-annotations/033-function-types.problem.ts" resourceId="1fZdJK1AI9JNeRElmqAeqb"></Exercise>
20372032

20382033
#### Exercise 5: Functions Returning `void`
20392034

@@ -2063,7 +2058,6 @@ const addClickEventListener = (listener) => {
20632058
};
20642059

20652060
// ---cut---
2066-
20672061
addClickEventListener(
20682062
// @ts-expect-error
20692063
"abc",
@@ -2074,7 +2068,7 @@ This is triggering our `@ts-expect-error` directive.
20742068

20752069
How should `addClickEventListener` be typed so that each error is resolved?
20762070

2077-
<Exercise title="Exercise 5: Functions Returning `void`" filePath="/src/015-essential-types-and-annotations/034-functions-returning-void.problem.ts"></Exercise>
2071+
<Exercise title="Exercise 5: Functions Returning `void`" filePath="/src/015-essential-types-and-annotations/034-functions-returning-void.problem.ts" resourceId="1fZdJK1AI9JNeRElmqAf1s"></Exercise>
20782072

20792073
#### Exercise 6: `void` vs `undefined`
20802074

@@ -2175,7 +2169,7 @@ How can we type `data` as a number without changing the calls to `fetch` or `res
21752169

21762170
There are two possible solutions here.
21772171

2178-
<Exercise title="Exercise 7: Typing Async Functions" filePath="/src/015-essential-types-and-annotations/038-type-async-functions.problem.ts"></Exercise>
2172+
<Exercise title="Exercise 7: Typing Async Functions" filePath="/src/015-essential-types-and-annotations/038-type-async-functions.problem.ts" resourceId="1fZdJK1AI9JNeRElmqAfhD"></Exercise>
21792173

21802174
#### Solution 1: Optional Function Parameters
21812175

@@ -2242,7 +2236,6 @@ type User = {
22422236
};
22432237

22442238
// ---cut---
2245-
22462239
const modifyUser = (user: User[], id: string, makeChange: () => any) => {
22472240
return user.map((u) => {
22482241
if (u.id === id) {

book-content/chapters/05-unions-literals-and-narrowing.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ This way, we can ensure that each function only handles the cases it's supposed
125125
// @errors: 2345
126126
type PhysicalFormat = "LP" | "CD" | "Cassette";
127127
// ---cut---
128-
129128
const getAlbumFormats = (format: PhysicalFormat) => {
130129
// function body
131130
};
@@ -211,7 +210,6 @@ function move(direction: string, distance: number) {
211210
// Move the specified distance in the given direction
212211
}
213212
// ---cut---
214-
215213
move(
216214
// @ts-expect-error - "up-right" is not a valid direction
217215
"up-right",
@@ -657,7 +655,6 @@ if (!appElement) {
657655
}
658656

659657
// ---cut---
660-
661658
console.log(appElement);
662659
// ^?
663660

book-content/chapters/07-mutability.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ Using `let`, we can declare a variable `albumGenre` and assign it the value `"ro
2323
type AlbumGenre = "rock" | "country" | "electronic";
2424

2525
// ---cut---
26-
2726
let albumGenre = "rock";
2827

2928
const handleGenre = (genre: AlbumGenre) => {
@@ -39,7 +38,6 @@ Because `let` was used when declaring the variable, TypeScript understands that
3938
let albumGenre = "rock";
4039

4140
// ---cut---
42-
4341
albumGenre = "country";
4442
```
4543

@@ -52,7 +50,6 @@ We can fix the error above by assigning a specific type to the `let`:
5250
type AlbumGenre = "rock" | "country" | "electronic";
5351

5452
// ---cut---
55-
5653
let albumGenre: AlbumGenre = "rock";
5754

5855
const handleGenre = (genre: AlbumGenre) => {
@@ -95,7 +92,6 @@ type AlbumGenre = "rock" | "country" | "electronic";
9592
const albumGenre = "rock";
9693

9794
// ---cut---
98-
9995
albumGenre = "country";
10096
```
10197

@@ -234,7 +230,6 @@ const readOnlyWhiteAlbum: Readonly<Album> = {
234230
status: "staff-pick",
235231
};
236232
// ---cut---
237-
238233
readOnlyWhiteAlbum.genre = ["rock", "pop", "unclassifiable"];
239234
```
240235

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,6 @@ 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-
685684
type test = Expect<
686685
Equal<
687686
AllPrograms,

book-content/chapters/11-annotations-and-assertions.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,6 @@ In the third scenario, we're trying to use `satisfies` with `document.getElement
731731
// @errors: 1360 2344
732732
import { Equal, Expect } from "@total-typescript/helpers";
733733
// ---cut---
734-
735734
// Third Scenario
736735
const element = document.getElementById("app") satisfies HTMLElement;
737736

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,6 @@ Now that we've typed `DEBUG`, the error message has moved to be under `getState(
904904
import { Equal, Expect } from "@total-typescript/helpers";
905905
declare const DEBUG: {};
906906
// ---cut---
907-
908907
const state = DEBUG.getState();
909908

910909
type test = Expect<Equal<typeof state, { id: string }>>;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ const egoMirror: VinylSingle = {
261261
};
262262

263263
// ---cut---
264-
265264
const ego = egoMirror.tracks[0];
266265
// ^?
267266
const mirror = egoMirror.tracks[1];

book-content/chapters/15-designing-your-types.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,6 @@ This means that our examples above now work as expected:
538538
type ToArray<T> = T extends any[] ? T : T[];
539539

540540
// ---cut---
541-
542541
type Example = ToArray<string>;
543542
// ^?
544543

@@ -863,7 +862,6 @@ type Result<TResult, TError = Error> =
863862
};
864863

865864
// ---cut---
866-
867865
type BadExample = Result<
868866
{ id: string },
869867
// @ts-expect-error Should be an object with a message property

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ const createSet = <T = string>(arr?: T[]) => {
278278
return new Set(arr);
279279
};
280280
// ---cut---
281-
282281
const numberSet = createSet<number>([1, 2, 3]);
283282
// ^?
284283
```

0 commit comments

Comments
 (0)