Skip to content

TypeScript Tuples, Enums vs Literal Unions, satisfies and Template Literal Types

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

You can now type almost any value, but real code keeps using a few more shapes. A map location is exactly two numbers in a fixed order. A settings object should be checked against a type without losing the exact keys you wrote. An invoice number always starts with "INV-". And sooner or later you will open a codebase full of enum. This lesson covers these four tools: tuples, enums, the satisfies operator and template literal types. Each one is small, and each one replaces a comment such as "must be latitude first" or "use one of these codes" with a rule the compiler enforces.

Quick answer

A tuple is an array with a fixed length and a type per position: type Coords = [lat: number, lng: number]. An enum creates named constants and a real JavaScript object; most new code uses a literal union such as "pending" | "paid" instead, because it is plain data and works with type stripping. The satisfies operator checks a value against a type but keeps the value's own, more exact type: const rates = { AED: 1, SAR: 0.9793 } satisfies Record<Currency, number>. Template literal types describe string patterns, such as a type that accepts only INV- followed by a number.

35 min read and practice · Content checked on 18 November 2026

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.

Try it: Branch locations as labelled tuples
Code
ResultUpdates as you type
Console
  1. 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.