Rest Parameters
Rest parameters allow you to handle functions that accept an arbitrary number of arguments. You use rest parameters when you do not know in advance how many arguments a function will receive, or when you want to work with a flexible list of values. Rest parameters are represented by three dots (...) followed by a parameter name, and they collect all remaining arguments into a single array. This feature is especially useful for tasks like mathematical operations on variable-length input, collecting extra configuration options, or forwarding arguments to another function.
12345678function sumAll(...numbers: number[]): number { return numbers.reduce((sum, current) => sum + current, 0); } console.log(sumAll(1, 2, 3, 4)); // 10 console.log(sumAll(5)); // 5 console.log(sumAll()); // 0 — works fine, no numbers passed console.log(sumAll(10, -3, 2, 1)); // 10
When you use rest parameters in TypeScript, you must explicitly type them as an array. In the function sumAll, the rest parameter ...numbers is typed as number[], which means TypeScript will enforce that all arguments passed to the function must be numbers. If you try to pass a non-number argument, TypeScript will generate a type error. Rest parameters always appear at the end of the parameter list, and you can only have one rest parameter per function. This typing ensures that you can safely use array methods on the collected arguments and that your code remains type-safe and predictable.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Can you give more examples of using rest parameters in TypeScript?
What happens if I try to use multiple rest parameters in a function?
How do rest parameters differ from the `arguments` object in JavaScript?
Awesome!
Completion rate improved to 9.09
Rest Parameters
Svep för att visa menyn
Rest parameters allow you to handle functions that accept an arbitrary number of arguments. You use rest parameters when you do not know in advance how many arguments a function will receive, or when you want to work with a flexible list of values. Rest parameters are represented by three dots (...) followed by a parameter name, and they collect all remaining arguments into a single array. This feature is especially useful for tasks like mathematical operations on variable-length input, collecting extra configuration options, or forwarding arguments to another function.
12345678function sumAll(...numbers: number[]): number { return numbers.reduce((sum, current) => sum + current, 0); } console.log(sumAll(1, 2, 3, 4)); // 10 console.log(sumAll(5)); // 5 console.log(sumAll()); // 0 — works fine, no numbers passed console.log(sumAll(10, -3, 2, 1)); // 10
When you use rest parameters in TypeScript, you must explicitly type them as an array. In the function sumAll, the rest parameter ...numbers is typed as number[], which means TypeScript will enforce that all arguments passed to the function must be numbers. If you try to pass a non-number argument, TypeScript will generate a type error. Rest parameters always appear at the end of the parameter list, and you can only have one rest parameter per function. This typing ensures that you can safely use array methods on the collected arguments and that your code remains type-safe and predictable.
Tack för dina kommentarer!