
Rest Parameters
February 8, 2025About 2 min
Rest Parameters 관련
Learn TypeScript – A Handbook for Developers
This handbook will teach you the basics of TypeScript, including what it is, why it is useful, and the key features it offers. TypeScript was created by Anders Hejlsberg, a prominent software engineer at Microsoft who’s also known for his work on C# ...
Learn TypeScript – A Handbook for Developers
This handbook will teach you the basics of TypeScript, including what it is, why it is useful, and the key features it offers. TypeScript was created by Anders Hejlsberg, a prominent software engineer at Microsoft who’s also known for his work on C# ...
Rest parameters in TypeScript let you handle multiple arguments without knowing how many you’ll get in advance. You can pass as many arguments as you want—TypeScript will handle them. They’re perfect for situations where the number of inputs isn’t fixed.
To use rest parameters, you write three dots (...
) before the parameter name, which gathers all the extra arguments into an array.
Let’s say you want to combine multiple words into a single sentence:
function joinWords(...words: string[]): string {
return words.join(" ");
}
let sentence = joinWords("TypeScript", "makes", "coding", "fun");
console.log(sentence); // "TypeScript makes coding fun"
...words
collects all the arguments into an array (["TypeScript", "makes", "coding", "fun"]
).- The
join
method combines them into a single string, separated by spaces.
Rest Parameters with Numbers
Now, suppose you want to add multiple numbers:
function sumNumbers(...numbers: number[]): number {
return numbers.reduce((total, num) => total + num, 0);
}
let total = sumNumbers(10, 20, 30);
console.log(total); // 60
...numbers
gathers all the numbers into an array ([10, 20, 30]
).- The
reduce
method adds them together to get the total.
We can also use rest parameters to merge multiple arrays into one:
function mergeArrays(...arrays: number[][]): number[] {
return arrays.flat();
}
let combined = mergeArrays([1, 2], [3, 4], [5, 6]);
console.log(combined); // [1, 2, 3, 4, 5, 6]
...arrays
collects each argument as an array into an array of arrays ([[1, 2], [3, 4], [5, 6]]
).- The
flat
method combines them into one array.
Rest parameters must always come last in the parameter list. For example:
function example(a: string, ...others: number[]): void {
console.log(a, others);
}
This ensures all remaining arguments go into the rest parameter.