Essential Types And Notations 20 exercises
solution

Using Rest Parameters in TypeScript

As we've seen with previous errors involving an implicit any type, the solution here is relatively straight forward.

When using rest parameters, all of the arguments passed to the parameter will end up as an array that is passed to the function. So in this case, the strings parameter can be typ

Loading solution

Transcript

00:00 So the solution here is relatively simple. We know that this is just an array of strings because that's how rest parameters work in JavaScript. If you specify it like this, then all of the arguments passed to that rest parameter will end up as an array passed to the function. So we can just type this as an array.

00:17 So strings is an array of strings here. If we were to, let's say, remove these three little dots before strings, then we would have to actually pass in a single argument, which would be the array. So we could wrap inside the function call to concatenate,

00:34 we could wrap this with an array, and now it works fine. But in order to get our API right, then we actually want it to be just this sort of, you can pass in as many arguments as you want. And you can also specify things before this as well. So you can have, for instance, a number before this and this before the rest parameter,

00:52 and then you would have to pass one number into here, and then the rest would be strings. So this is how you type rest parameters in TypeScript.