Accessing Tuple Elements
What you'll learn: How TypeScript remembers the specific type of each position in a tuple when you access elements by index.
The Magic of Positional Types
When you access an element in a regular array, TypeScript tells you "this could be any element of the array's type." But tuples are special—they remember which type belongs to which position. When you access tuple[0], TypeScript knows exactly what type that first element is, not just that it's "one of the types in the tuple."
Think of a tuple like a form with labeled boxes: the first box always holds a string, the second always holds a number, and so on. When you reach into box zero, you know you're getting a string—no guessing required.
How It Works
let point: [number, number] = [10, 20];
let x = point[0]; // Type: number
let y = point[1]; // Type: number
let mixed: [string, number, boolean] = ["hello", 42, true];
let text = mixed[0]; // Type: string
let count = mixed[1]; // Type: number
let flag = mixed[2]; // Type: boolean
Each index access gives you the exact type declared at that position. You can immediately call string methods on mixed[0], do math with mixed[1], and use mixed[2] in conditionals—TypeScript knows what each one is.
This is different from arrays, where every element has the same type. With tuples, position matters, and TypeScript tracks it perfectly.
Key Takeaway: Accessing a tuple element by index gives you that position's specific declared type, providing precise type safety for each slot in your fixed-length structure.