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

Basic Variadic Tuple Syntax

Using ...T[] syntax to represent variable-length portions of tuple types.

Basic Variadic Tuple Syntax

What you'll learn: How to use the ...T[] syntax to represent variable-length portions within tuple types, enabling flexible type-safe patterns for collections with mixed fixed and dynamic elements.

The Core Concept

In regular tuple types, you define a fixed number of elements with specific positions and types. But what if you need a tuple where part of it has a known structure, while another part can contain any number of elements of a certain type?

Variadic tuple syntax lets you combine fixed tuple elements with variable-length segments using the spread operator (...) in type definitions.

Understanding the Syntax

The ...T[] syntax (where T is some type) represents "zero or more elements of type T" within a tuple type. This creates a variadic (variable-argument) section in your tuple.

// A tuple with a fixed string at the start, 
// then any number of numbers
type StartsWithString = [string, ...number[]];

const valid1: StartsWithString = ["hello"];
const valid2: StartsWithString = ["hello", 1];
const valid3: StartsWithString = ["hello", 1, 2, 3];

You can also place variadic elements at different positions:

// Any number of strings, followed by a fixed boolean
type EndsWithBoolean = [...string[], boolean];

const valid4: EndsWithBoolean = [true];
const valid5: EndsWithBoolean = ["a", "b", true];

Or even sandwich variadic elements between fixed ones:

// Fixed number, variable strings, fixed boolean
type Sandwich = [number, ...string[], boolean];

const valid6: Sandwich = [42, true];
const valid7: Sandwich = [42, "middle", true];
const valid8: Sandwich = [42, "a", "b", "c", true];

Why This Matters

Variadic tuples unlock type safety for function signatures and data structures that mix predictable and flexible elements—like event handlers with a fixed context followed by dynamic arguments, or log messages with metadata plus variable data.

Key Takeaway: The ...T[] syntax in tuple types represents a variable-length segment of elements of type T, allowing you to create tuple types that combine fixed positions with flexible, repeating portions.