Rest Parameters
What
Rest parameters allow a function to accept an indefinite number of final arguments, which are then collected into a single array inside the function body.
How
You denote a rest parameter using three dots (...) before the parameter name. The type must be an array type:
function gatherParty(partyName: string, ...adventurers: string[]): string {
return `${partyName} consists of: ${adventurers.join(", ")}`;
}
const msg = gatherParty("The Fellowship", "Frodo", "Sam", "Gandalf");
Why
Rest parameters provide a clean way to handle functions that can take a variable number of inputs (like console.log or a math sum function). They are more type-safe and readable than the legacy JavaScript arguments object.
Summary
A function can only have one rest parameter, and it must be the very last parameter in the signature.