How to Use Rest Parameters in TypeScript

Use rest parameters in TypeScript when a function should accept any number of values as one typed array. This is perfect for math helpers, logging utilities, string builders, and reusable APIs.

What you’ll build or solve

You’ll learn how to use rest parameters in TypeScript with typed arrays, tuples, and spread-friendly helpers. You’ll also know how rest parameters differ from normal arrays.

When this approach works best

This approach is the right choice when the number of arguments is flexible.

Common real-world scenarios include:

  • Sum helpers
  • Logger utilities
  • Route builders
  • Merge helpers
  • Batch validators

This is a bad idea when the function expects a fixed known number of arguments.

Prerequisites

You only need:

  • Basic TypeScript function syntax
  • Familiarity with arrays
  • Understanding of type annotations

Step-by-step instructions

Step 1: Add a typed rest parameter

The syntax uses ... before the parameter name.

function sum(...numbers: number[]): number {
  return numbers.reduce((total, current) => total + current, 0);
}

This collects all extra arguments into a typed array.

sum(10, 20, 30);

The function still stays strongly typed.

Step 2: Use rest parameters in arrow functions

The same pattern works with arrow functions.

const joinWords = (...words: string[]): string => {
  return words.join(" ");
};

This is great for flexible string builders.

Step 3: Combine fixed and rest parameters

A function can mix required values with the rest array.

function logMessage(level: string, ...messages: string[]): void {
  console.log(level, messages);
}

This keeps the first argument fixed while the rest stay flexible.

What to look for:

  • ... collects extra arguments
  • The rest parameter becomes a typed array
  • Great for flexible APIs
  • Must be the last parameter
  • Works with arrow functions too

Examples you can copy

Number sum

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

Join strings

const join = (...parts: string[]) => parts.join("-");

Logger

function log(level: string, ...messages: string[]) {}

Common mistakes and how to fix them

Mistake 1: Forgetting the array type

What the reader might do:

function sum(...numbers: number)

Why it breaks: rest parameters must be typed as arrays.

Corrected approach:

function sum(...numbers: number[])

Mistake 2: Putting the rest parameter first

What the reader might do:

function log(...messages: string[], level: string)

Why it breaks: rest parameters must be last.

Corrected approach:

Place fixed parameters first.

Mistake 3: Using rest for fixed APIs

What the reader might do:

Use ...values for exactly 2 numbers.

Why it breaks: the function contract becomes less clear.

Corrected approach:

Use fixed typed parameters.

Troubleshooting

If TypeScript errors, confirm the rest parameter uses an array type.

If arguments map incorrectly, make sure the rest parameter is last.

If the API expects a fixed count, switch to explicit parameters.

If tuple shapes matter, consider typed tuple rest patterns.

Quick recap

  • Rest parameters collect flexible arguments
  • Type them as arrays
  • They must be the last parameter
  • Great for flexible utilities
  • Use fixed params when the count is known