Rest parameters allow a function to accept an indefinite number of arguments as an array.
Getting Started
What is TypeScript?
Basic Types
Types & Interfaces
Async TypeScript
Promises & Async/Await
TypeScript
Intermediate
FREE
Rest Parameters
Lesson 3 of 17
Intermediate
Interactive
Syntax
TYPESCRIPT
function sum(...numbers: number[]): number { return numbers.reduce((a, b) => a + b, 0); }
Rest Parameters
TYPESCRIPT
function sum(...numbers: number[]): number { return numbers.reduce((a, b) => a + b, 0); } function log(level: string, ...messages: string[]): void { console.log(`[${level}]`, ...messages); } console.log(sum(1, 2, 3, 4, 5)); log("INFO", "Server started", "on port 3000");
Practice
1
Exercise
Practice this concept.
Answer
Write the code as shown above.
Quick Quiz
1
What did you learn?
This covers the basics.
Interview Questions
It is a fundamental TypeScript feature.