What are Rest Parameters?
Rest parameters allow a function to accept an indefinite number of arguments as an array.
Basic Syntax
TypeScriptRead-only
1
Calling Function
TypeScriptRead-only
1
With Other Parameters
TypeScriptRead-only
1
Rest vs Spread
| Feature | Rest | Spread |
|---|---|---|
| Usage | Function parameters | Function calls |
| Purpose | Collect arguments | Expand elements |
Spread Example
TypeScriptRead-only
1
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.