What is a tuple in TypeScript?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for TypeScript development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
A tuple is an array with a fixed number of elements where each element has a specific type at a specific position. Unlike plain arrays, tuples enforce both the number of elements and the type at each index. Example: let point: [number, number] = [10, 20];. Mixed types: let entry: [string, number, boolean] = ["Alice", 30, true];. Access: entry[0] is typed as string, entry[1] as number. Tuples can have optional elements: [string, number?] and rest elements: [string, ...number[]]. Named tuples (TS 4.0+) improve readability: type Range = [start: number, end: number]. Common use cases: React's useState returns a tuple ([value, setter]), coordinates ([lat, lng]), key-value pairs. TypeScript cannot prevent you from pushing extra elements to a tuple via array methods — use readonly tuples (readonly [number, number]) for truly immutable tuples.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last TypeScript project, I used this when...' immediately makes your answer more credible and memorable.