How do you type a GeoJSON Coordinate tuple and LineString array?
Tests fixed-length tuple typing versus flexible arrays in TypeScript. A strong answer uses [number, number] for Coordinate, then Coordinate[] for LineString, and explains why number[] loses length safety. Red flag: using number[] or objects instead of tuples.
WHAT THIS TESTS: This question tests whether you understand the difference between array types and tuple types in TypeScript. Arrays are for collections of unknown or variable length, while tuples model fixed-length structures where each position has a distinct semantic meaning. GeoJSON coordinates are a classic real-world tuple because the API contract guarantees exactly two numbers in a specific order: longitude first, latitude second. Using the wrong type erases that contract and allows invalid data.
A GOOD ANSWER COVERS: A good answer hits four things in order. First, define Coordinate as a tuple: type Coordinate = [number, number];. This enforces exactly two elements and preserves positional semantics. Second, define LineString as an array of those tuples: type LineString = Coordinate[]; or equivalently Array<Coordinate>. Third, explain why number[] is wrong for a Coordinate because it allows any length including zero, one, or three-plus elements, and it does not protect against swapping lon and lat at the type level. Fourth, optionally mention readonly tuples like readonly [number, number] if immutability matters, or even a branded type to prevent accidentally passing a raw number pair where a Coordinate is expected.
COMMON WRONG ANSWERS: The most common wrong answer is type Coordinate = number[] because candidates default to flexible arrays without thinking about fixed-length constraints. Another red flag is interface Coordinate { longitude: number; latitude: number } which ignores the actual API shape and would require runtime mapping. Some candidates write type Coordinate = [number, number][] and conflate a single coordinate with a LineString, showing confusion about nesting levels. A subtle miss is forgetting that TypeScript tuples are open to excess elements unless you use a length-checking trick, though for most interviews [number, number] is sufficient.
LIKELY FOLLOW-UPS: An interviewer might ask how to enforce that a LineString has at least two points, which could lead to a rest-tuple or a custom generic like type AtLeastTwo<T> = [T, T, ...T[]]. They might also ask how to type a Polygon, which is an array of LineStrings where the first and last Coordinate must match, or how to handle z-coordinates and make the altitude optional in a 3D tuple like [number, number, number?]. Another follow-up is readonly versus mutable tuples and when each is appropriate for API data.
ONE CONCRETE EXAMPLE: Imagine validating a GPS trace. With type Coordinate = [number, number]; you can destructure safely: function format([lon, lat]: Coordinate) { return lon + "," + lat; }. If you typed it as number[], lon and lat would still be numbers but the function would accept [1] or [1, 2, 3] without complaint. For LineString, type LineString = Coordinate[]; lets you map over segments: lineString.map(([lon, lat]) => ...). If you used number[][], you would lose the guarantee that each inner array has exactly two elements, forcing defensive runtime checks.
Read the original → typescriptlang.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.