Ch. 2 · TypeScript

TypeScript Tuples for Fixed Position Data

TypeScript Tuples for Fixed Position Data. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readintermediateupdated Oct 3, 2026

Tuples encode positional meaning and expected length. Arrays describe repeated elements without fixed positions. Choose tuples when order is part of the contract.

Before you start

You should be comfortable with JavaScript values, functions and objects. Use a TypeScript project with strict checking enabled when trying the examples. Separate what the compiler proves from what must still be checked when values arrive at runtime.

The practical goal is to reason through this situation: A coordinate [number, number] records exactly two positions, unlike number[]. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Identify positional meaning

A coordinate has exactly an x and y position. An unrestricted number array does not communicate that length or interpretation.

Step 2: Choose tuple or object

Use labels to document positions. Choose an object when named access or future optional fields matter more than positional interoperability.

Step 3: Check runtime producers

A tuple annotation does not reshape a network array. Validate length and element types before accepting external values as coordinates.

Worked scenario

A coordinate [number, number] records exactly two positions, unlike number[].

type Point = [x: number, y: number];
function distance([x, y]: Point): number {
  return Math.hypot(x, y);
}
console.log(distance([3, 4])); // 5
// distance([3]); // missing second coordinate
// distance([3, 4, 5]); // unexpected third coordinate
TypeScript

Labels document the two positions but do not create x and y properties on the runtime array. Destructuring gives the local variables their readable names.

Common mistake

Labels improve readability but do not create runtime field names.

Verify the behavior

Try one, two and three elements and a string coordinate. If tuple values come from JSON, implement runtime checks for all of those cases.

Interview exercise

Represent a three-part result clearly.

Answer and reasoning

Use a labeled tuple for positional APIs or an object when names and extensibility are more valuable.

Continue learning

Compare the scenario with the TypeScript interview questions and test your understanding with the TypeScript MCQs. For terminology and implementation details, consult the reference material.

More in TypeScript

read ✓TypeScript · hard

TypeScript Abstract Classes and Contracts

Share behavior with abstract classes, enforce required members, and decide when an interface or composition is the better contract.

~2 min readread →
read ✓TypeScript · mid

TypeScript Enums vs Union Types

Compare enums with unions of string literals: runtime cost, serialization, exhaustiveness and which one fits application code.

~3 min readread →
esc