Learn
Arrays
Rest Parameters

Assigning types to rest parameters is similar to assigning types to arrays. Here’s a rest parameter example without types:

function smush(firstString, ...otherStrings){ let output = firstString; for(let i = 0; i < otherStrings.length; i++){ output = output.concat(otherStrings[i]); } return output; }

This function concatenates all of its arguments. For example, calling: smush('hi ', 'there') returns the value 'hi there'.” The rest parameter otherStrings lets the function work with any number of parameters greater than zero:

smush('a','h','h','H','H','H','!','!'); // Returns: 'ahhHHH!!'.


The function works well, but it is not type safe. We don’t want a misguided coder to make a mistake like smush(1,2,3), when that would cause an error. TypeScript to the rescue! Type annotations for a rest parameter are identical to type annotations for arrays. The function with a correctly typed rest parameter is then:

function smush(firstString, ...otherStrings: string[]){ /*rest of function*/ }

With this change, TypeScript will treat otherStrings as an array of strings. This means that smush(1,2,3) will result in a type error because [2,3] is not an array of strings.

Now, it’s your turn to write a rest parameter type!

Instructions

1.

The code editor includes another function using rest parameters. The function, addPower(p, ...numsToAdd), adds up the pth powers of all the subsequent arguments. So the call addPower(2, 3, 4) returns 25, since 3 squared plus 4 squared is 25. (Here, we are using the exponentiation operator **.)

Please add the appropriate type annotations for this function. Include the annotations for all arguments and the return type.

2.

There is no such thing as a number to the power of 'a string'. Verify that the function results in a type error when you add the code addPower('a string', 4, 5, 6) and run tsc.

Folder Icon

Take this course for free

Already have an account?