0Pricing
TypeScript Academy · Lesson

Rest Parameters and Spread with Types

Type rest parameters and spread operations.

Rest Parameters and Spread with Types is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

Rest parameters collect multiple arguments into an array. TypeScript types the rest parameter as an array of the element type.

Typing Rest Parameters

Use `...name: T[]` syntax for a rest parameter. It captures all remaining arguments as an array.
function sum(...nums: number[]): number {
  return nums.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3, 4)); // 10

Rest After Required Parameters

Rest parameters must be last. Other required parameters come before them.
function log(level: string, ...messages: string[]): void {
  console.log(level, messages.join(' '));
}
log('INFO', 'Server', 'started', 'on', 'port', '3000');

Spreading Arrays into Functions

Use spread `...` to pass array elements as individual arguments. TypeScript checks that the spread type matches the parameters.
function add(a: number, b: number): number { return a + b; }
const args: [number, number] = [1, 2];
add(...args); // OK — tuple spread is type-safe

Spreading with Regular Arrays

Spreading a plain array into a fixed-parameter function can cause an error because TypeScript cannot verify the count matches.
const nums = [1, 2]; // number[] — length unknown
// add(...nums); // Error: spread must match parameters
const pair: [number, number] = [1, 2];
add(...pair); // OK — tuple has known length

Rest Parameters vs arguments Object

Rest parameters replace the old `arguments` object. They are real arrays with full TypeScript type support, unlike `arguments`.

Spreading Objects (Not Rest Params)

Spreading an object creates a shallow copy. TypeScript infers the combined type of the spread result.
const a = { x: 1, y: 2 };
const b = { z: 3 };
const c = { ...a, ...b }; // { x: number; y: number; z: number }

Overriding Properties with Spread

When spreading, later properties override earlier ones with the same name.
const base = { color: 'red', size: 5 };
const updated = { ...base, color: 'blue' };
// { color: 'blue', size: 5 }

Typed Spread in Arrays

Array spread creates a new array. TypeScript infers the combined element type.
const a: number[] = [1, 2];
const b: number[] = [3, 4];
const c: number[] = [...a, ...b]; // [1, 2, 3, 4]

Variadic Tuple Rest

TypeScript supports rest elements in tuple types for advanced variadic patterns.
type Strings = string[];
type StringsAndNumber = [...Strings, number];
const t: StringsAndNumber = ['a', 'b', 42];

Rest in Destructuring

Use rest syntax in destructuring to collect remaining elements into a typed array.
const [first, ...rest]: number[] = [1, 2, 3, 4];
// first: number, rest: number[]

Quick Check

What type does TypeScript infer for the rest parameter `...items` in `function collect(...items: string[])`?

Recap

Rest parameters (`...args: T[]`) collect remaining arguments as a typed array. Spread arrays into functions using tuples for type safety. Spread objects to create shallow copies with inferred combined types.

Frequently asked questions

Is the “Rest Parameters and Spread with Types” lesson free?

Yes — the full text of “Rest Parameters and Spread with Types” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Rest Parameters and Spread with Types”?

Type rest parameters and spread operations. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Rest Parameters and Spread with Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Parameter and Return Type Annotations
  2. Optional and Default Parameters
  3. Rest Parameters and Spread with Types
  4. void, never, and Function Signatures
← Back to TypeScript Academy