Recall & Review
beginner
What are rest parameters in TypeScript?
Rest parameters allow a function to accept an indefinite number of arguments as an array. They are declared using three dots (...) before the parameter name.
Click to reveal answer
beginner
How do you type rest parameters in TypeScript?
You specify the type of the rest parameter as an array type. For example, <code>function sum(...numbers: number[]) {}</code> means <code>numbers</code> is an array of numbers.Click to reveal answer
intermediate
Can rest parameters have different types in TypeScript?
Yes, you can use union types or tuples to type rest parameters with different types. For example, <code>function example(...args: (string | number)[]) {}</code> accepts strings or numbers.Click to reveal answer
intermediate
What is the difference between rest parameters and regular array parameters?
Rest parameters gather all remaining arguments into an array automatically, while regular array parameters expect a single array argument. Rest parameters make calling functions easier when passing multiple values.
Click to reveal answer
beginner
Show a simple example of a function using typed rest parameters.
Example:<br><pre>function greetAll(...names: string[]) {<br> names.forEach(name => console.log(`Hello, ${name}!`));<br>}<br>greetAll('Alice', 'Bob', 'Charlie');</pre>Click to reveal answer
How do you declare a rest parameter named
items that accepts numbers in TypeScript?✗ Incorrect
Rest parameters use three dots before the parameter name and the type is an array type, so
...items: number[] is correct.What type does a rest parameter have inside the function?
✗ Incorrect
Rest parameters collect all extra arguments into an array of the specified type.
Which of these is a valid rest parameter type for accepting strings or numbers?
✗ Incorrect
The rest parameter must be typed as an array of union types:
(string | number)[].Can a function have more than one rest parameter?
✗ Incorrect
Functions can only have one rest parameter and it must be the last parameter.
What happens if you call a function with rest parameters without any arguments?
✗ Incorrect
If no arguments are passed, the rest parameter is an empty array.
Explain how to use rest parameters with types in a TypeScript function.
Think about how to accept many arguments as one array with a type.
You got /3 concepts.
Describe the rules and limitations of rest parameters in TypeScript.
Consider function parameter order and typing.
You got /4 concepts.