typescript
/

TypeScript Rest Parameters

Last Sync: Today

On this page

10
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

typescript

TypeScript Rest Parameters

What are Rest Parameters?

Rest parameters allow a function to accept an indefinite number of arguments as an array.

Basic Syntax

TypeScriptRead-only
1
function sum(...numbers: number[]): number {
  return numbers.reduce((a, b) => a + b, 0);
}

Calling Function

TypeScriptRead-only
1
sum(1, 2, 3, 4);

With Other Parameters

TypeScriptRead-only
1
function greet(message: string, ...names: string[]) {
  return names.map(n => `${message} ${n}`);
}

Rest vs Spread

FeatureRestSpread
UsageFunction parametersFunction calls
PurposeCollect argumentsExpand elements

Spread Example

TypeScriptRead-only
1
let nums = [1, 2, 3];
sum(...nums);

Rules

  • Only one rest parameter allowed
  • Must be the last parameter
  • Always treated as an array

Best Practices

  • Use rest for flexible arguments
  • Define proper array types
  • Keep function logic simple
  • Avoid overusing rest parameters

Common Mistakes

  • Placing rest parameter incorrectly
  • Using multiple rest parameters
  • Ignoring type definitions
  • Confusing rest with spread

Conclusion

Rest parameters provide flexibility in handling multiple arguments in TypeScript functions while maintaining type safety.

Try it yourself

function sum(...nums: number[]) {
  return nums.reduce((a, b) => a + b);
}

Test Your Knowledge

Q1
of 3

Rest symbol?

A
...
B
??
C
&&
D
||
Q2
of 3

Rest used in?

A
Call
B
Parameters
C
Loop
D
Object
Q3
of 3

Rest must be?

A
First
B
Last
C
Middle
D
Any

Frequently Asked Questions

What is rest parameter?

It collects multiple arguments into an array.

Where to place rest?

At the end of parameters.

Difference from spread?

Rest collects, spread expands.

Previous

ts optional default

Next

ts interfaces

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.