In this lesson you will
- Use tuples, with labels, for fixed-length values such as coordinates
- Read enums and choose a literal union or an as const object instead
- Check configuration objects with satisfies without losing exact types
- Describe string formats with template literal types
Tuples: fixed length, fixed order
An ordinary array type such as number[] says nothing about length: it may hold zero items or a thousand. A tuple type lists each position: [number, number] is exactly two numbers, and [string, number] is a string followed by a number. Add labels to say what each position means, [lat: number, lng: number]; they appear in your editor and make mistakes such as swapping latitude and longitude easier to spot when reading. Reading coords[2] from a two-item tuple is an error, and so is creating one with three numbers.
Tuples are the natural type for a function that returns two things, as React's useState does with [value, setValue], and they pair well with destructuring: const [lat, lng] = branch.location. Mark a tuple readonly when it should not change, and an element optional with ?, as in [name: string, phone?: string]. Keep tuples short: once you have more than two or three positions, an object with named properties reads better than remembering what row[4] was.
Console
- Console messages, errors, form data and clicked links appear here.
The function destructures each tuple into named values. Line 19 has three numbers where the tuple allows two, and line 20 reads a third position that does not exist; both are compile errors, while in plain JavaScript the second would just print undefined.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What is a tuple in TypeScript?
An array type with a fixed number of items and a type for each position, such as [string, number]. At run time it is an ordinary array; the length and order are checked only by the compiler. Labels like [lat: number, lng: number] document each position.
Should I use enums in TypeScript?
You can, and you will meet them in existing code. For new code, a union of string literals, often built from an as const array, is usually simpler: it works with plain strings from JSON, generates no code and is allowed under erasableSyntaxOnly.
What does the satisfies operator do in TypeScript?
It checks that a value matches a type, reporting missing or wrong properties, without changing the type of the value itself. Use it for configuration objects and lookup tables where you want both the check and the exact keys. It was added in TypeScript 4.9.
What are template literal types?
String types written with backticks and placeholders, such as a type for "INV-" followed by a number, or a type built from two unions that lists every combination. They check the format of text at compile time; they do not validate data that arrives at run time.
What does keyof typeof mean?
typeof obj in a type position gives the type of the value obj, and keyof then takes the union of its property names. Together they turn the keys of a real object into a type, so the type updates whenever you add a key to the object.
Sources
Facts in this lesson were checked against these official pages on 18 November 2026.
- TypeScript Handbook: Object Types (tuple types)
- TypeScript Handbook: Enums
- TypeScript 4.9 release notes: the satisfies operator
- TypeScript Handbook: Template Literal Types
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.