Chapter 30: TypeScript Literal Types

Literal Types in TypeScript very calmly and step by step, like we’re sitting together with VS Code open, chai on the table, and we’re typing small examples one by one until everything feels crystal clear.

Literal types are one of the features that make TypeScript feel magical when used correctly — they turn vague string / number into very precise, almost compile-time constants.

1. What exactly is a literal type? (the simplest honest explanation)

A literal type is a type that represents exactly one concrete value — not a whole category.

TypeScript

Compare with normal types:

TypeScript

So literal types are extremely narrow — they describe one single possible value.

2. Most common and useful form: string literal union

Almost everyone first meets literal types as unions of string literals:

TypeScript

This pattern is extremely common in 2025–2026 code:

  • button variants
  • API status/response kinds
  • event names
  • theme modes
  • user roles
  • HTTP methods
  • form input types
  • Tailwind/size/color enums

Realistic everyday example:

TypeScript

3. Literal types exist for string, number, boolean, bigint

TypeScript

4. Literal types + as const — the most powerful daily combo

When you write an object/array literal, TypeScript normally widens the values:

TypeScript

But very often you want literal narrowness (especially for configs, routes, enums-like objects, theme tokens).

Solution → as const

TypeScript

Even better — combine with type-level extraction:

TypeScript

This pattern (const object + as const + keyof typeof) replaced most enum usages after ~2020.

5. Very common real patterns in 2025–2026 codebases

Pattern 1: Status / result kinds (discriminated unions)

TypeScript

Pattern 2: Route / path literals

TypeScript

Pattern 3: Theme / design tokens

TypeScript

Pattern 4: Button / component variants

TypeScript

6. Quick summary table — Literal types cheat sheet

Kind Example syntax Widened without as const Narrow with as const Most common use-case
String literal “loading” string “loading” Status, variants, events, roles
Number literal 200 number 200 Status codes, magic numbers (rare)
Boolean literal true boolean true Flags that should only be one way
Object literal { status: “ok” } as const { status: string } { readonly status: “ok” } Configs, constants, theme objects
Array literal [“left”, “right”] as const string[] readonly [“left”, “right”] Fixed choices, directions, days of week
Union of literals “GET” | “POST” | “PUT” Almost every enum-like value in modern TS

Your mini homework (try right now in playground)

  1. Create type Size = “xs” | “sm” | “md” | “lg” | “xl”
  2. Make a const sizes = { xs: “0.75rem”, sm: “0.875rem”, … } as const
  3. Extract type SizeKey = keyof typeof sizes
  4. Extract type SizeValue = (typeof sizes)[SizeKey]
  5. Try assigning wrong value → watch the beautiful red underline

Which part feels most useful for your current project?

Want to go deeper into:

  • as const vs traditional enum
  • Literal types in React props / Tailwind / shadcn/ui
  • Combining literal unions with discriminated unions
  • Literal types in generics / conditional types / mapped types
  • Common mistakes people still make with literals

Just tell me — we’ll zoom in right there! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *