Rest Parameters
π¨βπΌ Perfect! You now understand both spread and rest.
π¦ Remember the difference:
- Spread (
...arr) - Expands an array/object into individual elements - Rest (
...args) - Collects individual elements into an array
// REST: Collecting arguments
function sum(...nums: Array<number>) {
// SPREAD: Expanding back for another call
return Math.max(...nums)
}
Rest parameters are type-safe in TypeScript:
// TypeScript knows this is Array<number>
function add(...numbers: Array<number>): number {
return numbers.reduce((a, b) => a + b, 0)
}
add(1, 2, 3) // β
add(1, 'two') // β Type error!
This completes the spread/rest storyβyou now have all the tools for immutable
data manipulation in TypeScript!