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.
The ...nums collects all arguments into an array: [1, 2, 3, 4].
Step 2: Calculate sum using reduce
The reduce adds all numbers: 1+2=3, 3+3=6, 6+4=10.
Final Answer:
10 -> Option D
Quick Check:
Sum of 1,2,3,4 = 10 [OK]
Hint: Rest gathers numbers; reduce sums them [OK]
Common Mistakes:
Concatenating numbers as strings
Forgetting initial value in reduce
Misunderstanding rest parameter as single number
4. Identify the error in this TypeScript function:
function joinStrings(...strings: string) {
return strings.join(", ");
}
medium
A. The function should return an array, not a string.
B. The join method cannot be used on strings.
C. Rest parameter must be typed as an array, e.g., string[].
D. Rest parameter cannot be used with strings.
Solution
Step 1: Check rest parameter type
The rest parameter must be typed as an array type, like string[], not just string.
Step 2: Understand why join fails
Since strings is typed as a single string, join method is invalid.
Final Answer:
Rest parameter must be typed as an array, e.g., string[]. -> Option C
Quick Check:
Rest parameter type = array type [OK]
Hint: Rest parameter type must be an array type [OK]
Common Mistakes:
Typing rest parameter as a single element type
Trying to call array methods on a non-array
Ignoring TypeScript type errors
5. You want to create a TypeScript function multiplyAll that accepts any number of numeric arguments and returns their product. Which is the best way to type the rest parameter and implement this function?
hard
A. function multiplyAll(nums: number[]): number { return nums.reduce((a, b) => a * b); }
B. function multiplyAll(...nums: number[]): number { return nums.reduce((a, b) => a * b, 1); }
C. function multiplyAll(...nums: number): number { return nums.reduce((a, b) => a * b, 1); }
D. function multiplyAll(...nums: any[]): any { return nums.reduce((a, b) => a * b, 0); }
Solution
Step 1: Choose correct rest parameter typing
The rest parameter must be typed as number[] to accept multiple numbers.
Step 2: Implement product with correct initial value
Use reduce with initial value 1 to multiply all numbers correctly.
Step 3: Evaluate options
function multiplyAll(...nums: number[]): number { return nums.reduce((a, b) => a * b, 1); } matches correct typing and logic. function multiplyAll(nums: number[]): number { return nums.reduce((a, b) => a * b); } lacks rest parameter. function multiplyAll(...nums: number): number { return nums.reduce((a, b) => a * b, 1); } has wrong type. function multiplyAll(...nums: any[]): any { return nums.reduce((a, b) => a * b, 0); } uses any and wrong initial value.
Final Answer:
function multiplyAll(...nums: number[]): number { return nums.reduce((a, b) => a * b, 1); } -> Option B
Quick Check:
Rest parameter typed number[] and reduce with 1 = correct [OK]
Hint: Rest parameter as number[] and reduce with 1 for product [OK]
Common Mistakes:
Using single number type instead of array for rest
Forgetting initial value in reduce
Using any type losing type safety
Starting reduce with 0 causing product always 0
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.