Rest parameters allow functions to accept a variable number of arguments by collecting them into a single array.

- Accept a variable number of arguments.
- Collect additional arguments into an array.
- Must be the last parameter in a function definition.
Syntax:
function functionName(...rest: type[]): returnType {
// function body
}
- functionName: Name of the function.
- ...rest: Collects all remaining arguments into an array.
- type[]: Specifies the type of elements in the rest parameter.
Examples of Rest Parameters
The following examples demonstrate how rest parameters accept and process a variable number of arguments.
Example 1: Summing Numbers
Rest parameters make it easy to process multiple values passed to a function.
function sum(...numbers: number[]): number {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3));
console.log(sum(10, 20));
Output:
6
30
- ...numbers collects all arguments into a number[].
- reduce() calculates the sum of all elements.
- The function accepts any number of numeric arguments.
Example 2: Calculating the Average of Numbers
Rest parameters can be used to perform calculations on a variable number of values.
function average(...numbers: number[]): number {
let total = 0;
for (let num of numbers) {
total += num;
}
return numbers.length === 0 ? 0 : total / numbers.length;
}
console.log("Average of the given numbers is:", average(10, 20, 30, 60));
console.log("Average of the given numbers is:", average(5, 6));
console.log("Average of the given numbers is:", average(4));
Output:
Average of the given numbers is: 30
Average of the given numbers is: 5.5
Average of the given numbers is: 4
- ...numbers accepts any number of numeric arguments.
- The function calculates their average.
- It returns 0 when no arguments are provided.
Example 3: Concatenating Strings
Rest parameters can also collect multiple string arguments into a single array.
function joinStrings(...strings: string[]): string {
return strings.join(', ');
}
console.log(joinStrings("rachel", "john", "peter") + " are mathematicians");
console.log(joinStrings("sarah", "joseph") + " are coders");
Output:
rachel, john, peter are mathematicians
sarah, joseph are coders
- ...strings collects all string arguments.
- join() combines them into a comma-separated string.
- The function accepts any number of strings.
Example 4: Incorrect Usage of Rest Parameters
A rest parameter must always be the last parameter in a function definition.
// Incorrect usage - will raise a compiler error
function job(...people: string[], jobTitle: string): void {
console.log(`${people.join(', ')} are ${jobTitle}`);
}
// Uncommenting the below line will cause a compiler error
// job("rachel", "john", "peter", "mathematicians");
Output: TypeScript compiler raised the error.
main.ts(2,14): error TS1014: A rest parameter must be last in a parameter list. - ...people is placed before jobTitle.
- TypeScript requires the rest parameter to be the last parameter.
- Otherwise, a compile-time error occurs.