Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Type Operators

TypeScript spread operator (...)

Reference for the TypeScript spread operator: spreading arrays, tuples, objects and call arguments, how types are computed and what is copied.

The spread operator ... expands an iterable or an object in place. In an array literal it inserts the elements, in an object literal it copies own enumerable properties, and in a call it passes the elements as separate arguments. The same three dots also mean "rest" when used in a parameter or destructuring pattern.

TypeScript tracks the shape of the result. Spreading tuples keeps their positions, spreading objects merges property types with later properties overriding earlier ones, and spreading arguments into a call is checked against the parameter list.

TypeScript
const a = [1, 2] as const;
const b = [...a, 3]; // number[]
const o = { ...{ x: 1 }, y: "two" }; // { y: string; x: number }

Try it

TypeScript Arrays, tuples and calls

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Object spread and override order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
[...iterable, other]
{ ...object, key: value }
fn(...args)
type T = [...A, ...B]
Position Description
Array literal Elements of any iterable are inserted in order. Needs downlevelIteration or a target of ES2015 or later for non-array iterables.
Object literal Own enumerable properties are copied. Methods and getters on a prototype are not. Getters are read and the value copied.
Call arguments The source must be an array or tuple of known length when the parameters are fixed. A plain number[] is rejected for a fixed-arity function.
Tuple type [...A, ...B] concatenates tuple types; a generic T extends unknown[] can be spread too.

Result type: for objects, the intersection-like merged shape where the rightmost property wins. For arrays, the union of element types.

Errors: spreading a non-iterable in an array ([...5]) is a compile error; A spread argument must either have a tuple type or be passed to a rest parameter appears when a non-tuple array is spread into fixed parameters.

Usage notes

  • Shallow only. Nested objects and arrays are shared between the original and the copy.
  • Order matters in object spread: { ...a, x: 1 } forces x, while { x: 1, ...a } lets a.x override it. If a.x is optional, the result type becomes the union.
  • Class instances lose their prototype when spread into a literal; the result is a plain object.
  • Large arrays passed with fn(...big) can exceed the engine's argument limit; use a loop or reduce instead.
  • Variadic tuples: spreading generic tuples ([...T, number]) was added in TypeScript 4.0 and lets you type concat, curry and similar helpers.
  • Rest is the mirror image: function f(...args: number[]) and const [first, ...rest] = list collect values; see destructuring.