Skip to main content

Tuples

What a tuple is

A tuple is a fixed-length array where each position has its own type. A normal array says "many of one type"; a tuple says "these exact types, in this exact order."

type Entry = [string, number];

const a: Entry = ["ajay", 23]; // Valid: string then number
const b: Entry = [23, "ajay"]; // Error: wrong order
const c: Entry = ["ajay"]; // Error: wrong length

Where you meet tuples in real code

Object.entries returns tuples — each entry is a [key, value] pair — which is why destructuring in the loop works with correct types:

const person = { name: "ajay", age: 23 };

Object.entries(person).forEach(([key, val]) => {
console.log(key, val); // key: string, val: string | number
});

console.log(Object.entries(person));
// [ ["name", "ajay"], ["age", 23] ] — an array of tuples

The most familiar tuple: React's useState

useState returns a tuple — value first, setter second — which is why the array destructuring names can be anything you like:

const [count, setCount] = useState(0);
// return type is roughly: [number, (n: number) => void]

A tuple is an array with a schema per slot: fixed length, position-specific types. Use it for pairs and small fixed records like [key, value] or [state, setState].