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.

0 / 39 solved

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

  1. 31Type destructured parametersWhen you pull things out of an object or array, tell TypeScript what type each piece is.Go to the exercises
  2. 32Type rest parameters...scores: number[] — type the "everything else" parameter as an array.Go to the exercises

Map and Set

  1. 33Type Map and SetMap<string, number> and Set<string> — write the angle brackets yourself this time.Go to the exercises

Next up is “Type async code and APIs”. Promise<T> for return types, interface for API shapes, unknown for data you cannot predict.