ProgramGuru

Rest Parameters

Understand Rest Parameters

A rest parameter gathers zero or more remaining arguments into an array. It is declared with ... and must be last.

What you will learn: the purpose of rest parameters, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how rest parameters works and where its type rules apply.
  • Predict the exact output of the rest parameters example.
  • Diagnose a common issue: Do not write (.

Build the mental model

Its annotation is an array or tuple type, not the type of one element. Rest parameters and spread arguments are related but different: the rest parameter collects; a spread expands existing iterable values into a call.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Rest Parameters: explained visually2, 3, 5 → Rest → [2,3,5] → Reduce → 102, 3, 5Rest → [2,3,5]Reduce → 10Follow the values through the operation
Rest Parameters: 2, 3, 5 → Rest → [2,3,5] → Reduce → 10.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

function total(...values: number[]): number {
  return values.reduce((sum, value) => sum + value, 0);
}
console.log(total(2, 3, 5));
console.log(total());
const amounts = [4, 6];
console.log(total(...amounts));

Verified output

10
0
10

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

The first call gathers [2,3,5] and returns 10. No arguments create an empty array, and reduce starts at 0. Spreading [4,6] supplies two arguments and returns 10.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Do not write (...values: number); the variable inside the body is an array. Rest must be the final parameter.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Create average(...numbers) and decide what to return when no numbers are provided.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

What is values inside total?
Choose one answer. You can try again.

Answer and explanation

A number[] containing all supplied arguments.

A rest parameter gathers zero or more remaining arguments into an array. It is declared with ... and must be last.

Review the key idea

Rest Parameters: A rest parameter gathers zero or more remaining arguments into an array.

The first call gathers [2,3,5] and returns 10. No arguments create an empty array, and reduce starts at 0. Spreading [4,6] supplies two arguments and returns 10.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Function Overloading →