You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book-content/chapters/04-essential-types-and-annotations.md
-2Lines changed: 0 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,3 @@
1
-
# 04. Essential Types and Annotations
2
-
3
1
Now we've covered most of the why of TypeScript, it's time to start with the how. We'll cover key concepts like type annotations and type inference, as well as how to start writing type-safe functions.
4
2
5
3
It's important to build a solid foundation, as everything you'll learn later builds upon what you'll learn in this chapter.
Copy file name to clipboardExpand all lines: book-content/chapters/05-unions-literals-and-narrowing.md
+40Lines changed: 40 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -179,6 +179,11 @@ Normally we wouldn't explicitly call the `getUsername` function with `null`, but
179
179
180
180
Currently, the `username` parameter only accepts a `string` type, and the check for `null` isn't doing anything. Update the function parameter's type so the errors are resolved and the function can handle `null` .
Here we have a `move` function that takes in a `direction` of type string, and a `distance` of type number:
@@ -227,6 +232,11 @@ move(
227
232
228
233
Your challenge is to update the `move` function so that it only accepts the strings `"up"`, `"down"`, `"left"`, and `"right"`. This way, TypeScript will throw an error when we try to pass in any other string.
229
234
235
+
<Exercise
236
+
title="Exercise 2: Restricting Function Parameters"
There are several different ways to validate the username length.
@@ -921,6 +946,11 @@ console.log(error.message);
921
946
922
947
Your task is to update the `if` statement to have the proper condition to check if the `error` has a message attribute before logging it. Check the title of the exercise to get a hint... And remember, `Error` is a class.
923
948
949
+
<Exercise
950
+
title="Exercise 1: Narrowing Errors with `instanceof`"
Here we have a `parseValue` function that takes in a `value` of type `unknown`:
@@ -962,6 +992,11 @@ it("Should error when anything else is passed in", () => {
962
992
963
993
Your challenge is to modify the `parseValue` function so that the tests pass and the errors go away. I want you to challenge yourself to do this _only_ by narrowing the type of `value` inside of the function. No changes to the types. This will require a very large `if` statement!
964
994
995
+
<Exercise
996
+
title="Exercise 2: Narrowing `unknown` to a Value"
Let's imagine that we have two very similar functions, each with a long conditional check to narrow down the type of a value.
@@ -1010,6 +1045,11 @@ Both functions have the same conditional check. This is a great opportunity to c
1010
1045
1011
1046
All the tests are currently passing. Your job is to try to refactor the two functions to use a reusable type guard, and remove the duplicated code. As it turns out, TypeScript makes this a lot easier than you expect.
0 commit comments