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!

Please set the playground first

Loading "Rest Parameters"
Loading "Rest Parameters"