What is a tuple — and why useState returns an array of two

You've written this line a hundred times in React:
const [count, setCount] = useState(0);
Ever wondered why the square brackets? Why useState returns an array instead of an object like { value, setValue }? And why you can name the variables anything — [likes, setLikes], [x, y]?
The answer is one word: tuple. It's a small trick hiding inside the most popular hook. Let's see what it is — and find the spot where TypeScript quietly lets you down.
What a tuple is
A tuple is a short list of fixed length where each position has its own meaning.
A regular array is many similar things: a shopping list, a list of orders. How many and in what order doesn't matter much.
A tuple is the opposite. The order is the meaning. Real-life example — map coordinates: [55.75, 37.62]. The first number is latitude, the second longitude. Swap them and you're on the other side of the planet.
More examples:
[count, setCount]— a value and the function that changes it.["Anna", 25]— a name and an age.[200, "OK"]— a status code and its text.
Why useState is a tuple, not an object
Imagine useState returned an object { value, setValue }. For two counters you'd have to write:
const { value: likes, setValue: setLikes } = useState(0);
const { value: views, setValue: setViews } = useState(0);
Clunky. With a tuple you pick the names yourself, in one move:
const [likes, setLikes] = useState(0);
const [views, setViews] = useState(0);
It works through destructuring by position: first slot — the value, second — the setter. Names don't matter, order does. More about state itself in what is state.
Hence the rule: a tuple is great when there are 2–3 items with an obvious order. Five fields? Return an object. Nobody will remember that the fourth item means "is loading".
Where TypeScript won't save you
JavaScript has no separate tuple type — it's a plain array. TypeScript, though, can describe one strictly:
const user: [string, number] = ["Anna", 25];
user[2]; // error: the tuple has no third slot ✅
user.push(99); // but this one slips through 🤔
Surprise: TypeScript complains if you read an extra element, but allows push. After it, your "tuple of two" holds three values, and the compiler stays silent.
The cure is the word readonly:
const user: readonly [string, number] = ["Anna", 25];
user.push(99); // now it's an error ✅
The most common trap: a custom hook without as const
You ask the AI to move logic into your own hook. It writes:
function useToggle() {
const [on, setOn] = useState(false);
return [on, () => setOn(!on)];
}
const [isOpen, toggle] = useToggle();
toggle(); // error: "This expression is not callable"
Why? TypeScript doesn't know you meant a tuple. It just sees an array holding a boolean and a function mixed together. To it, toggle is "either a boolean or a function". You can't call that.
The fix is two words at the end:
return [on, () => setOn(!on)] as const;
as const says: "this is a tuple, slot one is a boolean, slot two is a function". The error goes away. If the AI wrote a custom hook and TS complains about calling the second item — check exactly this.
Where you'll meet tuples
- React hooks:
useState,useReducer, many library hooks. Object.entries— returns[key, value]pairs. Those are tuples too.- Python: there a tuple is a separate type in round brackets,
(55.75, 37.62), and you can't change it. - Functions that return "result and error":
const [data, error] = await safeFetch(url).
The most useful of these day to day is Object.entries. It turns an object into a list of tuples you can loop over:
const prices = { coffee: 250, tea: 180 };
for (const [name, price] of Object.entries(prices)) {
console.log(`${name}: ${price} ₽`);
}
// coffee: 250 ₽
// tea: 180 ₽
Each loop step gets a two-slot tuple and splits it into name and price right away. Same trick as useState, just in a loop.
Are tuples and arrays different things in JavaScript?
No. In JavaScript itself a tuple is a plain array. Only TypeScript — and you, when you agree on the order — see the difference.
Tuple or object — which to pick?
Two or three items with an obvious order — tuple. More, or the order isn't obvious — an object with named fields. An object is easier to read six months later.
Why can't you change a tuple in Python?
By design: Python tuples are immutable. That's why a tuple can be a dictionary key and a list can't.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





