AI Hints
Home›TypeScript Tutorial›More About Functions
TypeScript · Lesson 16 of 120

Rest Parameters

Many arguments.

⏱ 2 min read▶ Video at 13:14🎓 Free
Watch this lesson in the TypeScript courseWatch on YouTube ↗Full TypeScript course

Some functions take any number of arguments. Three dots before the last parameter collect them all into an array. And that array has a type, like any other.

Rest Parameters
From the video: Rest Parameters
  • Any number of arguments
  • Three dots
  • A typed array

Adding any number of prices

Total collects all its arguments into prices, an array of numbers. We can call it with two prices, or four. Run it.

app.tstypescript
function total(...prices: number[]): number {
  return prices.reduce((sum, p) => sum + p, 0);
}

console.log(total(10, 20));
console.log(total(849.5, 1299, 450, 25));
Output
30
2623.5

Summary

Three dots collect extra arguments into a typed array. Next, function types.

Key takeaways
  • ...prices
  • number[]
  • Any count
Next up: Function Types — functions as values