Course contentsShow
← TypeScript
Lesson 1379 of 1,79632. Advanced PatternsFree lesson

Spreading Tuples into Other Tuples

Combining tuples with spread syntax and how TypeScript merges the types.

What You'll Learn

You'll learn how to combine multiple tuple types using the spread operator (...) and how TypeScript intelligently merges their types into a single, precise tuple type.

The Core Concept

Just like you can spread arrays into other arrays in JavaScript, you can spread tuple types into other tuple types in TypeScript. The key insight: TypeScript preserves the exact positional types from both tuples in the resulting combined type.

Think of it like stacking LEGO instruction steps in sequence. If step 1 requires [red brick, blue brick] and step 2 requires [yellow brick], spreading them together gives you instructions for [red brick, blue brick, yellow brick] — maintaining the exact order and type of each piece.

How TypeScript Merges Tuple Types

When you spread tuples together, TypeScript concatenates their element types in order:

type Coordinates = [number, number];
type ElevationData = [number];

// Spreading creates a new tuple with all elements in sequence
type Point3D = [...Coordinates, ...ElevationData];
// Result: [number, number, number]

const point: Point3D = [10, 20, 5]; // ✓ Valid

The spread happens positionally — the first tuple's elements come first, followed by the second tuple's elements.

Combining Different Element Types

You can spread tuples with different types, and TypeScript tracks each position precisely:

type UserInfo = [string, number]; // [name, age]
type LocationInfo = [string, string]; // [city, country]

type FullProfile = [...UserInfo, ...LocationInfo];
// Result: [string, number, string, string]

const profile: FullProfile = ["Alice", 30, "London", "UK"];

Spreading with Optional and Rest Elements

Optional tuple elements and rest elements spread as you'd expect:

type RequiredFields = [string, number];
type OptionalField = [boolean?];

type Combined = [...RequiredFields, ...OptionalField];
// Result: [string, number, boolean?]

const valid1: Combined = ["test", 42];
const valid2: Combined = ["test", 42, true];

Key Takeaway: Spreading tuples combines their types sequentially and precisely, maintaining the exact position and type of each element, enabling you to compose complex fixed-length array types from simpler tuple building blocks.