TypeScript Tuples

TypeScript tutorial

TypeScript Tuples

In TypeScript Tuples is a special type of array where:
  • Number of elements is fixed

  • Type of each element is predefined

  •  Order cannot be changed

Tuples are useful when you need a structured collection of values.

 Declaring a Tuple

  • Index 0 → number

  • Index 1 → string

 Invalid:

 Tuple with More Elements

 Tuple with Optional Elements

 Readonly Tuples

Prevents modification.

Not allowed:

Named Tuple Elements (Recommended)

Improves readability.

Tuple Destructuring

Tuple destructuring in TypeScript allows extracting values from a tuple into separate variables in a single statement. Since tuples have fixed positions and types, destructuring follows the exact order of elements. For example, if a tuple contains a string and a number, destructuring will assign the first value to one variable and the second to another. This improves readability and makes it easy to access tuple elements without using index positions repeatedly.

 

 Tuple vs Array

A tuple is a fixed-length array where each element has a specific type and position, whereas an array can have any number of elements of the same type. In a tuple, the order and type of values are strictly defined. In an array, elements are generally of one common type and the length can change dynamically. Tuples are useful when returning multiple related values with different types, while arrays are better for collections of similar data.

FeatureTupleArray
LengthFixedDynamic
TypeFixed per indexSame type
OrderImportantNot strict
Use CaseStructured dataLists

 Tuple in Functions

Tuples are commonly use in functions to return multiple values with different types. Instead of returning an object, a function can return a tuple to maintain order and type safety. When receiving a tuple from a function, it can be destructured directly into variables. Tuples can also be use as function parameters when a fixed structure of arguments is required.

Tuple with Rest Elements

TypeScript supports rest elements in tuples, allowing flexible-length structures while keeping fixed starting elements. A tuple can define specific initial types and then use a rest element to include additional values of a certain type. This is helpful when the first few values are fixing but the remaining values can vary in number.

 Tuple Summary

Tuples in TypeScript provide a way to define fixed-length arrays with predefined types for each position. They ensure type safety, maintain order, and are useful for returning multiple values from functions. Destructuring improves readability, and rest elements add flexibility. Tuples are ideal when structure and order matter, while arrays are better for uniform, dynamic collections.

FeatureExample
Basic Tuple[number, string]
Optional[number, string?]
Readonlyreadonly [number, number]
Named[id: number, name: string]
Rest[string, ...number[]]

Best Practices

  •  Use tuples for fixed structured data
  •  Prefer named tuple elements
  •  Use readonly for safety
  • Avoid tuples for large dynamic lists

You may also like...