Skip to content

Commit b2fdf49

Browse files
committed
Added exercises to 02 and 04
1 parent 9364297 commit b2fdf49

File tree

4 files changed

+126
-2
lines changed

4 files changed

+126
-2
lines changed

book-content/chapters/02-ide-superpowers.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# 02. IDE Superpowers
2-
31
TypeScript works the same no matter what IDE you use, but for this book we'll assume you're using Visual Studio Code (VS Code).
42

53
When you open VS Code, the TypeScript server starts in the background. It will be active as long as you have a TypeScript file open.
@@ -71,6 +69,8 @@ acceptsObj({
7169

7270
Practice using the autocomplete shortcut to fill in the object when calling `acceptsObj`.
7371

72+
<Exercise title="Exercise 1: Autocomplete" filePath="/src/016.5-ide-superpowers/044-manually-triggering-autocomplete.problem.ts" />
73+
7474
#### Solution 1: Autocomplete
7575

7676
When you hit `Ctrl + Space` inside the object, you'll see a list of the possible properties based on the `MyObj` type:
@@ -282,6 +282,8 @@ let element = document.getElementById(12);
282282

283283
How can hovering help to determine what argument `document.getElementById` actually requires? And for a bonus point, what type is `element`?
284284

285+
<Exercise title="Exercise 1: Hovering a Function Call" filePath="/src/016.5-ide-superpowers/041-hovering-a-function-call.problem.ts" />
286+
285287
#### Solution 1: Hovering a Function Call
286288

287289
First of all, we can hover over the red squiggly error itself. In this case, hovering over `12`, we get the following error message:
@@ -361,6 +363,8 @@ In order to understand what this function does, you'd have to read the code.
361363

362364
Add some documentation to the function so that when you hover over it, you can read a description of what it does.
363365

366+
<Exercise title="Exercise 1: Adding Documentation For Hovers" filePath="/src/016.5-ide-superpowers/042-adding-tsdoc-comments-for-hovers.problem.ts" />
367+
364368
#### Solution 1: Adding Documentation for Hovers
365369

366370
In this case, we'll specify that the function "Adds two numbers together". We can also use an `@example` tag to show an example of how the function is used:
@@ -508,6 +512,8 @@ Highlight a variable, line, or entire code block then hit `Command + .` to open
508512

509513
Experiment with different options to see how they affect the example function.
510514

515+
<Exercise title="Exercise 1: Quick Fix Refactoring" filePath="/src/016.5-ide-superpowers/050-refactor.problem.ts" />
516+
511517
### Solution 1: Quick Fix Refactoring
512518

513519
The Quick Fix menu will show different refactoring options depending on where your cursor is when you open it.

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ Finally, we can see that our `test` result has an error because the `result` is
285285

286286
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.
287287

288+
<Exercise
289+
title="Exercise 1: Basic Types with Function Parameters"
290+
filePath="/src/015-essential-types-and-annotations/020-basic-types-with-function-parameters.problem.ts"
291+
/>
292+
288293
#### Exercise 2: Annotating Empty Parameters
289294

290295
Here we have a `concatTwoStrings` function that is similar in shape to the `add` function. It takes two parameters, `a` and `b`, and returns a string.
@@ -317,6 +322,11 @@ type test = Expect<Equal<typeof result, string>>;
317322

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

325+
<Exercise
326+
title="Exercise 2: Annotating Empty Parameters"
327+
filePath="/src/015-essential-types-and-annotations/021-annotating-empty-parameters.problem.ts"
328+
/>
329+
320330
#### Exercise 3: The Basic Types
321331

322332
As we've seen, TypeScript will show errors when types don't match.
@@ -352,6 +362,11 @@ Type 'boolean' is not assignable to type 'string'.
352362

353363
Change the types of the annotations on each variable to make the errors go away.
354364

365+
<Exercise
366+
title="Exercise 3: The Basic Types"
367+
filePath="/src/015-essential-types-and-annotations/022-all-types.problem.ts"
368+
/>
369+
355370
#### Exercise 4: The `any` Type
356371

357372
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:
@@ -410,6 +425,11 @@ Why is this error happening? Why isn't TypeScript giving us an error here?
410425

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

428+
<Exercise
429+
title="Exercise 4: The `any` Type"
430+
filePath="/src/015-essential-types-and-annotations/032.5-any.problem.ts"
431+
/>
432+
413433
#### Solution 1: Basic Types with Function Parameters
414434

415435
Common sense tells us that the `boolean`s in the `add` function should be replaced with some sort of `number` type.
@@ -598,6 +618,11 @@ We can tell from the `concatName` function body that it expects `user.first` and
598618

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

621+
<Exercise
622+
title="Exercise 1: Object Literal Types"
623+
filePath="/src/015-essential-types-and-annotations/025-object-literal-types.problem.ts"
624+
/>
625+
601626
#### Exercise 2: Optional Property Types
602627

603628
Here's a version of the `concatName` function that has been updated to return just the first name if a last name wasn't provided:
@@ -645,6 +670,11 @@ The error tells us that we are missing a property, but the error is incorrect. W
645670

646671
How would you update this function to fix the errors?
647672

673+
<Exercise
674+
title="Exercise 2: Optional Property Types"
675+
filePath="/src/015-essential-types-and-annotations/026-optional-property-types.problem.ts"
676+
/>
677+
648678
#### Solution 1: Object Literal Types
649679

650680
In order to annotate the `user` parameter as an object, we can use the curly brace syntax `{}`.
@@ -829,6 +859,11 @@ Even though everything is working as expected, there's an opportunity for refact
829859

830860
How could you use the `type` keyword to make this code more readable?
831861

862+
<Exercise
863+
title="Exercise 1: The `type` Keyword"
864+
filePath="/src/015-essential-types-and-annotations/027-type-keyword.problem.ts"
865+
/>
866+
832867
#### Solution 1: The `type` Keyword
833868

834869
You can use the `type` keyword to create a `Rectangle` type with `width` and `height` properties:
@@ -1039,6 +1074,11 @@ As the error message points out, there is not currently a property called `items
10391074

10401075
How would you fix this error?
10411076

1077+
<Exercise
1078+
title="Exercise 1: Array Type"
1079+
filePath="/src/015-essential-types-and-annotations/028-arrays.problem.ts"
1080+
/>
1081+
10421082
#### Exercise 2: Arrays of Objects
10431083

10441084
Consider this `processRecipe` function which takes in a `Recipe` type:
@@ -1074,6 +1114,11 @@ Object literal may only specify known properties, and 'ingredients' does not exi
10741114

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

1117+
<Exercise
1118+
title="Exercise 2: Arrays of Objects"
1119+
filePath="/src/015-essential-types-and-annotations/029-arrays-of-objects.problem.ts"
1120+
/>
1121+
10771122
#### Exercise 3: Tuples
10781123

10791124
Here we have a `setRange` function that takes in an array of numbers:
@@ -1127,6 +1172,11 @@ setRange([0, 10, 20]);
11271172

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

1175+
<Exercise
1176+
title="Exercise 3: Tuples"
1177+
filePath="/src/015-essential-types-and-annotations/031-tuples.problem.ts"
1178+
/>
1179+
11301180
#### Exercise 4: Optional Members of Tuples
11311181

11321182
This `goToLocation` function takes in an array of coordinates. Each coordinate has a `latitude` and `longitude`, which are both numbers, as well as an optional `elevation` which is also a number:
@@ -1153,6 +1203,11 @@ const goToLocation = (coordinates: Array<number>) => {
11531203

11541204
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.
11551205

1206+
<Exercise
1207+
title="Exercise 4: Optional Members of Tuples"
1208+
filePath="/src/015-essential-types-and-annotations/032-optional-members-of-tuples.problem.ts"
1209+
/>
1210+
11561211
#### Solution 1: Array Type
11571212

11581213
For the `ShoppingCart` example, defining an array of `item` strings would looks like this when using the square bracket syntax:
@@ -1433,6 +1488,11 @@ new () => Map<any, any> (+3 overloads)
14331488

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

1491+
<Exercise
1492+
title="Exercise 1: Passing Types to Map"
1493+
filePath="/src/015-essential-types-and-annotations/036-pass-types-to-map.problem.ts"
1494+
/>
1495+
14361496
#### Exercise 2: `JSON.parse()` Can't Receive Type Arguments
14371497

14381498
Consider the following code, which uses `JSON.parse` to parse some JSON:
@@ -1485,6 +1545,11 @@ The test errors tell us that the type of `parsed` is not what we expect. The pro
14851545

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

1548+
<Exercise
1549+
title="Exercise 2: `JSON.parse()` Can't Receive Type Arguments"
1550+
filePath="/src/015-essential-types-and-annotations/037-json-parse-cant-receive-type-arguments.problem.ts"
1551+
/>
1552+
14881553
#### Solution 1: Passing Types to Map
14891554

14901555
There are a few different ways to solve this problem, but we'll start with the most straightforward one.
@@ -1848,6 +1913,11 @@ const result2 = concatName("John");
18481913

18491914
Try to use an optional parameter annotation to fix the error.
18501915

1916+
<Exercise
1917+
title="Exercise 1: Optional Function Parameters"
1918+
filePath="/src/015-essential-types-and-annotations/023-optional-function-parameters.problem.ts"
1919+
/>
1920+
18511921
#### Exercise 2: Default Function Parameters
18521922

18531923
Here we have the same `concatName` function as before, where the `last` name is optional:
@@ -1904,6 +1974,11 @@ expect(result).toEqual("John Pocock");
19041974

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

1977+
<Exercise
1978+
title="Exercise 2: Default Function Parameters"
1979+
filePath="/src/015-essential-types-and-annotations/024-default-function-parameters.problem.ts"
1980+
/>
1981+
19071982
#### Exercise 3: Rest Parameters
19081983

19091984
Here we have a `concatenate` function that takes in a variable number of strings:
@@ -1931,6 +2006,11 @@ The test passes, but there's an error on the `...strings` rest parameter.
19312006

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

2009+
<Exercise
2010+
title="Exercise 3: Rest Parameters"
2011+
filePath="/src/015-essential-types-and-annotations/030-rest-parameters.problem.ts"
2012+
/>
2013+
19342014
#### Exercise 4: Function Types
19352015

19362016
Here, we have a `modifyUser` function that takes in an array of `users`, an `id` of the user that we want to change, and a `makeChange` function that makes that change:
@@ -2003,6 +2083,11 @@ modifyUser(
20032083

20042084
How would you type `makeChange` as a function takes in a `User` and returns a `User`?
20052085

2086+
<Exercise
2087+
title="Exercise 4: Function Types"
2088+
filePath="/src/015-essential-types-and-annotations/033-function-types.problem.ts"
2089+
/>
2090+
20062091
#### Exercise 5: Functions Returning `void`
20072092

20082093
Here we explore a classic web development example.
@@ -2042,6 +2127,11 @@ This is triggering our `@ts-expect-error` directive.
20422127

20432128
How should `addClickEventListener` be typed so that each error is resolved?
20442129

2130+
<Exercise
2131+
title="Exercise 5: Functions Returning `void`"
2132+
filePath="/src/015-essential-types-and-annotations/034-functions-returning-void.problem.ts"
2133+
/>
2134+
20452135
#### Exercise 6: `void` vs `undefined`
20462136

20472137
We've got a function that accepts a callback and calls it. The callback doesn't return anything, so we've typed it as `() => undefined`:
@@ -2070,6 +2160,11 @@ acceptsCallback(returnString);
20702160

20712161
Why is this happening? Can we alter the type of `acceptsCallback` to fix this error?
20722162

2163+
<Exercise
2164+
title="Exercise 6: `void` vs `undefined`"
2165+
filePath="/src/015-essential-types-and-annotations/034.5-void-vs-undefined.problem.ts"
2166+
/>
2167+
20732168
#### Exercise 7: Typing Async Functions
20742169

20752170
This `fetchData` function awaits the `response` from a call to `fetch`, then gets the `data` by calling `response.json()`:
@@ -2139,6 +2234,11 @@ How can we type `data` as a number without changing the calls to `fetch` or `res
21392234

21402235
There are two possible solutions here.
21412236

2237+
<Exercise
2238+
title="Exercise 7: Typing Async Functions"
2239+
filePath="/src/015-essential-types-and-annotations/038-type-async-functions.problem.ts"
2240+
/>
2241+
21422242
#### Solution 1: Optional Function Parameters
21432243

21442244
By adding a question mark `?` to the end of a parameter, it will be marked as optional:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const acceptsCallback = (callback: () => undefined) => {
2+
callback();
3+
};
4+
5+
const returnString = () => {
6+
return "Hello!";
7+
};
8+
9+
acceptsCallback(returnString);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const acceptsCallback = (callback: () => void) => {
2+
callback();
3+
};
4+
5+
const returnString = () => {
6+
return "Hello!";
7+
};
8+
9+
acceptsCallback(returnString);

0 commit comments

Comments
 (0)