TypeScript Tuples
TypeScript Tuples: Fixed-Length Typed Sequences
A TypeScript tuple is an array with a fixed length where each position has its own specific type. Tuples work cleanly for coordinate pairs [lat, lng], CSV rows, and functions returning [value, error] pairs (like React's useState).
Without a tuple, [200, "OK"] infers as (string | number)[]—allowing any length and mixed order—whereas [number, string] enforces exact positions.
Named, Optional, and readonly Tuples
TypeScript 4.0+ lets you label tuple positions for clearer editor tooltips, and marking a tuple readonly blocks accidental .push() calls.
type ApiResult = readonly [status: number, body: string];
type Coordinate = [lat: number, lng: number, label?: string];
const response: ApiResult = [200, "Created"];
const office: Coordinate = [23.8103, 90.4125, "Dhaka HQ"];
const [statusCode, message] = response;
console.log(statusCode, message, office[0]);
Why readonly Matters on Tuples
Historically, TypeScript allowed calling .push() on a mutable tuple [number, string] without an error because tuples inherit Array.prototype. Adding readonly (or returning as const) shuts down .push() completely.
Common Mistakes to Avoid
- Forgetting
readonlyon returned tuples: Withoutreadonly, callers can mutate length with.push(). - Confusing
(string | number)[]with[string, number]: The first is an unbounded mixed array; the second requires exactly one string at index0and one number at index1. - Overusing long 6-element tuples: Once a record passes 3 fields, switch to a named
interfaceortypeobject so fields stay self-documenting.
Frequently Asked Questions
Can tuples use rest elements (...string[])?
Yes. [number, ...string[]] represents a tuple starting with one number followed by zero or more strings.
How do I create a tuple automatically with as const?
Writing const pair = [404, "Not Found"] as const infers readonly [404, "Not Found"] without writing the tuple type manually.
Why do labeled tuples fail if only some items have labels?
TypeScript requires either all elements in a tuple to have labels or none of them.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.