Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Rest Parameters | Advanced Parameters and Callbacks
TypeScript Functions and Parameters

bookRest 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.

12345678
function 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
copy

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.

question mark

Which statements about rest parameters in TypeScript are correct

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 9.09

bookRest Parameters

Deslize para mostrar o menu

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.

12345678
function 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
copy

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.

question mark

Which statements about rest parameters in TypeScript are correct

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 3
some-alt