CHAPTER 7 · 3 lessons · 39 exercises
Type modern syntax
Type destructured parameters, rest arguments and Map / Set — locking down the syntax you learnt in JavaScript.
You can now type variables, arrays, functions, objects and classes. But there are a few JavaScript patterns you learnt earlier that still have no types on them.
This chapter puts types on three things.
First, destructuring. Pulling values out of an object or array. In TypeScript the extracted variables get types. The most common place is function parameters — destructure the argument and type it in one go.
Second, rest parameters (...scores). The "take everything else" syntax gets an array type.
Third, Map and Set. Both use angle brackets. Map<string, number> and Set<string> work exactly like the Array<string> you read in chapter 6, but this time you write the brackets yourself.
Lessons in this chapter
Destructuring and rest
- 31Type destructured parametersWhen you pull things out of an object or array, tell TypeScript what type each piece is.Go to the exercises
- 32Type rest parameters
...scores: number[]— type the "everything else" parameter as an array.Go to the exercises
Map and Set
- 33Type Map and Set
Map<string, number>andSet<string>— write the angle brackets yourself this time.Go to the exercises