Rest Parameters and Spread with Types
Type rest parameters and spread operations.
Welcome
Rest parameters collect multiple arguments into an array. TypeScript types the rest parameter as an array of the element type.
Typing Rest Parameters
Use `...name: T[]` syntax for a rest parameter. It captures all remaining arguments as an array.
function sum(...nums: number[]): number {
return nums.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3, 4)); // 10All lessons in this course
- Parameter and Return Type Annotations
- Optional and Default Parameters
- Rest Parameters and Spread with Types
- void, never, and Function Signatures