Discover how to make your functions handle any number of inputs safely and easily!
Why Rest parameters with types in Typescript? - Purpose & Use Cases
Imagine you want to write a function that can take any number of numbers and add them all together. Without rest parameters, you have to write many versions of the function for different numbers of inputs or manually handle arrays.
This manual way is slow and confusing. You might forget to handle some cases or make mistakes counting inputs. Also, without types, you can accidentally pass wrong data, causing bugs that are hard to find.
Rest parameters with types let you collect all extra inputs into a typed array automatically. This means your function can accept any number of arguments, all checked by the type system, making your code simpler and safer.
function sum(a: number, b: number) { return a + b; } // only two numbers
function sum(...numbers: number[]) { return numbers.reduce((a, b) => a + b, 0); }
You can write flexible functions that handle many inputs safely and clearly, without extra code for each case.
Think of a calculator app where users can add any amount of numbers. Rest parameters with types let you write one function to handle all those inputs correctly.
Rest parameters gather multiple inputs into a typed array.
This makes functions flexible and type-safe.
It reduces errors and simplifies code.
Practice
...args: number[] do in a TypeScript function?Solution
Step 1: Understand rest parameter syntax
The...argsgathers all extra arguments passed to the function into one array.Step 2: Recognize the type annotation
The: number[]means this array contains only numbers.Final Answer:
It collects all remaining arguments into an array of numbers. -> Option AQuick Check:
Rest parameter with type = collects arguments as typed array [OK]
- Confusing rest parameter with spread operator
- Thinking it accepts only one argument
- Ignoring the type annotation on the rest parameter
Solution
Step 1: Identify correct rest parameter syntax
The rest parameter must use...before the parameter name and be typed as an array type.Step 2: Check each option
function greet(...names: string[]) {} uses...names: string[], which is correct. Others misuse the spread or array syntax.Final Answer:
function greet(...names: string[]) {} -> Option AQuick Check:
Rest parameter syntax = ...name: type[] [OK]
- Placing ... after the parameter name
- Using a single type instead of an array type
- Using tuple syntax instead of array type
function sum(...nums: number[]) {
return nums.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3, 4));Solution
Step 1: Understand rest parameter collects arguments
The...numscollects 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 DQuick Check:
Sum of 1,2,3,4 = 10 [OK]
- Concatenating numbers as strings
- Forgetting initial value in reduce
- Misunderstanding rest parameter as single number
function joinStrings(...strings: string) {
return strings.join(", ");
}Solution
Step 1: Check rest parameter type
The rest parameter must be typed as an array type, likestring[], not juststring.Step 2: Understand why join fails
Sincestringsis typed as a single string,joinmethod is invalid.Final Answer:
Rest parameter must be typed as an array, e.g., string[]. -> Option CQuick Check:
Rest parameter type = array type [OK]
- Typing rest parameter as a single element type
- Trying to call array methods on a non-array
- Ignoring TypeScript type errors
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?Solution
Step 1: Choose correct rest parameter typing
The rest parameter must be typed asnumber[]to accept multiple numbers.Step 2: Implement product with correct initial value
Usereducewith 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 BQuick Check:
Rest parameter typed number[] and reduce with 1 = correct [OK]
- 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
Ready to prove it?
Pass the test and this pattern moves unknown
