0
0
Typescriptprogramming~5 mins

Rest parameters with types in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Afunction example(...items: number[]) {}
Bfunction example(items: number...) {}
Cfunction example(items: ...number[]) {}
Dfunction example(...items: number) {}
What type does a rest parameter have inside the function?
AA union of all argument types
BA single value of the specified type
CA tuple of fixed length
DAn array of the specified type
Which of these is a valid rest parameter type for accepting strings or numbers?
A...args: (string | number)[]
B...args: string | number
C...args: string[] | number[]
D...args: string & number[]
Can a function have more than one rest parameter?
AYes, any number of rest parameters
BOnly if they are optional
CNo, only one rest parameter is allowed
DYes, but only if they have different types
What happens if you call a function with rest parameters without any arguments?
AThe rest parameter is undefined
BThe rest parameter is an empty array
CThe function throws an error
DThe rest parameter is null
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.