
Ts Tuples in TypeScript
A tuple in TypeScript is a type of array with a fixed number of elements, where each element has a specific type. Tuples are useful when you want to represent an array with a known length and type for each position.
Defining a Tuple
To define a tuple, use the square bracket []
notation with the types of its elements listed in order.
let person: [string, number];person = ["Alice", 30]; // Valid// person = [30, "Alice"]; // Error: Type 'number' is not assignable to type 'string'
Features of Tuples
Fixed Length
Tuples have a fixed number of elements. You cannot add or remove elements unless the tuple type allows it explicitly.Heterogeneous Types
Each element can have a different type.
Accessing Tuple Elements
Access tuple elements using their index, just like an array.
let point: [number, number] = [10, 20];console.log(point[0]); // Output: 10console.log(point[1]); // Output: 20
Optional Tuple Elements
You can make elements optional using the ?
operator.
let data: [string, number?];data = ["Alice"]; // Validdata = ["Alice", 42]; // Valid// data = [42]; // Error: Type 'number' is not assignable to type 'string'
Readonly Tuples
Make a tuple immutable using the readonly
modifier.
let coordinates: readonly [number, number] = [10, 20];// coordinates[0] = 15; // Error: Cannot assign to '0' because it is a read-only property
Using Tuples with Destructuring
Tuples work seamlessly with destructuring.
let employee: [string, number] = ["Bob", 35];let [name, age] = employee;console.log(name); // Output: "Bob"console.log(age); // Output: 35
Spread Operator with Tuples
You can use the spread operator to extract or merge tuples.
let tuple1: [number, string] = [1, "hello"];let tuple2: [boolean, ...typeof tuple1] = [true, ...tuple1];console.log(tuple2); // Output: [true, 1, "hello"]
Named Tuple Elements
TypeScript allows naming the elements in tuples to improve readability.
type Point = [x: number, y: number];let origin: Point = [0, 0];console.log(origin[0]); // Output: 0console.log(origin[1]); // Output: 0
Rest Elements in Tuples
Tuples can have rest elements to represent arrays of a specific type.
type StringTuple = [string, ...string[]];let words: StringTuple = ["hello", "world", "typescript"];
Use Cases for Tuples
Key-Value Pairs
let keyValue: [string, number] = ["id", 42];
Fixed-Structure Data
let date: [number, number, number] = [2025, 1, 18]; // Year, Month, Day
Return Multiple Values from a Function
function getCoordinates(): [number, number] { return [10, 20];}let [x, y] = getCoordinates();console.log(x, y); // Output: 10 20
Enumerate Data
let user: [id: number, name: string] = [1, "Alice"];
Best Practices
- Use tuples when the order and types of elements are fixed and meaningful.
- Prefer named tuples for improved code readability.
- Avoid using tuples for large or complex data structures; use interfaces or types instead.
- Use the
readonly
modifier for tuples that should not change after initialization.
Conclusion
Tuples in TypeScript provide a powerful way to work with arrays of fixed structure and types. They enable clarity and type safety for scenarios like function returns, key-value pairs, and more. By understanding their features and capabilities, you can leverage tuples effectively in your TypeScript projects.