CHALLENGE 3: Tuple for Coordinates ================================== SOLUTION: // Define the tuple type type Point = [number, number]; // Create the coordinates const origin: Point = [0, 0]; const pointA: Point = [5, 10]; const pointB: Point = [15, 20]; // Alternative (without a type alias): const origin: [number, number] = [0, 0]; const pointA: [number, number] = [5, 10]; const pointB: [number, number] = [15, 20]; // Even better with named elements: type NamedPoint = [x: number, y: number]; const origin: NamedPoint = [0, 0]; const pointA: NamedPoint = [5, 10]; const pointB: NamedPoint = [15, 20]; // Access coordinates: console.log(`Origin: x=${origin[0]}, y=${origin[1]}`); console.log(`Point A: x=${pointA[0]}, y=${pointA[1]}`); EXPLANATION: Why tuples instead of objects? - Tuples: lightweight, perfect for coordinate pairs - Objects: overkill if you only need x and y Why named elements matter: - [number, number] doesn't tell you which is x and which is y - [x: number, y: number] makes it crystal clear - The names serve as documentation TYPE SAFETY: ✅ Works: [0, 0] (correct: two numbers) ✅ Works: [5, 10] (correct: two numbers) ❌ Error: [5, 10, 15] (too many elements) ❌ Error: [5] (not enough elements) ❌ Error: [5, "10"] (second element should be number, not string) USE CASES FOR TUPLES: - Coordinates (x, y) or (x, y, z) - RGB colors [r, g, b] - API responses [status, data] - Key-value pairs (coming up in more advanced chapters) - Function return values with multiple outputs COMPARISON TO OBJECTS: Tuples: type Point = [number, number]; Objects: type Point = { x: number; y: number }; Tuples are great for fixed, small, ordered data. Objects are better when properties have names and might grow.