REST -Parameter in TypeScript
A rest parameter allows you a function to accept zero or more arguments
of the specified type. In TypeScript, rest parameters follow these rules:
1. A function has only one rest parameter.
2. The rest parameter appears last in the parameter list.
3. The type of the rest parameter is an array type.
// Example:
function getTotal(...numbers: number[]): number {
let total = 0;
numbers.forEach((num) => total += num);
return total;
}
console.log(getTotal()); // 0
console.log(getTotal(10, 20)); // 30
console.log(getTotal(10, 20, 30)); // 60
Tiny Coders