Essential Types And Notations 20 exercises
Problem

Rest Parameters in TypeScript

Here we have a concatenate function that takes in a variable number of strings:


export function concatenate(...strings) {
// red squiggly line under `...strings`
return strings.join("");
}
it("should concatenate strings", () => {
const result = concatenate("Hello", " ", "World")

Loading exercise

Transcript

00:00 In this exercise, we have a concatenate function and that concatenate function takes in a bunch of strings here. This means that we can pass concatenate, if we look at the test down here, as many arguments as we want to.

00:14 Currently, we're concatenating hello, a space, and then world, and we're expecting the result to equal hello world. Nice. So the question is, how do we represent that in TypeScript? This concatenate function, we can see we're using a rest parameter before it, but how do we give this a type? Because it's currently giving us

00:32 rest parameter strings implicitly has an any type. Good luck.