CHAPTER 4 · 4 lessons · 52 exercises

Guard your functions with types

Type arrow functions, optional arguments and the functions you pass around, so nothing breaks at the joins.

In chapter 1 you put types on a function. A : string after the parameter, the return type after the brackets. That covers the basics. In this chapter you put types, one at a time, on the various ways of writing a function you learnt in the JavaScript course.

First, the arrow function. You write it like const f = (n: number): number => n * 2;. The types of the parameters and the return go exactly where they did with function; there is just an arrow in between. Out in the wild you may well see this one more often.

Next, parameters you can leave out. Sometimes you want to call with just a name, sometimes with a title as well. Put a ? after the parameter name and it can be left out — but then, inside, you have to think about "it might not have been passed". TypeScript will not let you forget. There is a more straightforward way too, and it goes nicely with types: a default parameter. Write down what the value becomes when it is left out and you need not agonise inside.

And then the big moment of the chapter: handing a function around as a value. You learnt callbacks in the JavaScript course — passing a function as an argument to a function. The receiving side has to write in a type "what kind of function am I taking?". You write it as (n: number) => stringinside the brackets are the parameters, right of the arrow is the return. A function type looks almost exactly like a function.

Once you can write that, you can read the types of array methods like map and filter. TypeScript works out from the array type what the function you hand to map takes and returns. map over a string[] and the parameter of the function you handed over is understood to be a string without your writing it. This working out, called type inference, is where you feel most grateful for it.

Which is to say: you do not always have to write every type. It reads better when you leave out what is already obvious. This chapter touches on where to draw the line between "worth writing" and "worth leaving to it". There is one guide: write it at the border with other people. A function's parameters and return are a border, so write them. A temporary variable inside a function needs nothing.

By the end of this chapter you will hardly ever be stuck on types around functions. And once you have known the comfort of completion working inside a map, there is no going back to life without types.

Lessons in this chapter

Type every way of writing one

  1. 18Add types to an arrow functionWith the => shape too, the types of the parameters and the return go in the same places as with function.Go to the exercises
  2. 19Parameters you can leave outMake a parameter you do not have to pass with ?, and write out what happens when it is left out.Go to the exercises

Hand a function around

  1. 20Hand a function itself aroundA function type is written (n: number) => string. Inside the brackets are the parameters, right of the arrow is the return.Go to the exercises
  2. 21Array methods and inferred typesThe parameter of a function you hand to map or filter is settled by the array type, so you need not write it.Go to the exercises

Next up is “Add types to your classes”. Declare the type of each field, keep things safe with private, and promise a shape with implements.