Literal types
๐ What are Literal Types in TypeScript? A literal type is a type that is an exact specific value, not just any string or number. Instead of type string (any string), you can write 'red' | 'blue' | 'green' (only those three exact strings). TypeScript will then catch any typos or invalid values at cโฆ

Appy Saysโฆ
What if a variable shouldn't just be any string, but specifically 'north' | 'south' | 'east' | 'west'? TypeScript's literal types let you be that precise โ and the compiler catches any typo or invalid value instantly.
What are Literal Types?
Literal types restrict a value to one specific string, number, or boolean โ not just its base type. Combined with union types, they create precise allowed-value sets.
- โข
type Direction = 'north' | 'south' | 'east' | 'west'; - โข
type Status = 'idle' | 'loading' | 'success' | 'error'; - โข
type Dice = 1 | 2 | 3 | 4 | 5 | 6; - โข
type Flag = true | false;โ equivalent to boolean - โขTypeScript infers literal types from
const:const dir = 'north'โ type is'north' - โขUsing
let: type widens tostringunless you annotate
Think of it like Minecraft game modes
Minecraft's difficulty isn't 'any string' โ it's exactly 'Peaceful', 'Easy', 'Normal', or 'Hard'. Literal types work the same: you define the exact allowed values and TypeScript ensures nothing else can be assigned.
How It Works
- โข1. Define union of literals:
type Color = 'red' | 'green' | 'blue'; - โข2. Annotate:
let bg: Color = 'red'; - โข3.
bg = 'purple'โ compile error: 'purple' is not assignable to Color - โข4. Function parameter:
function setTheme(theme: 'light' | 'dark'): void - โข5. As const:
const THEMES = ['light', 'dark'] as const;โreadonly ['light', 'dark'] - โข6. Template literal types:
type Event = `on${Capitalize<string>}`
Real-World Examples
- โขHTTP methods:
'GET' | 'POST' | 'PUT' | 'DELETE' - โขNotification types:
'success' | 'warning' | 'error' | 'info' - โขButton variants:
'primary' | 'secondary' | 'ghost' | 'destructive' - โขUser roles:
'admin' | 'teacher' | 'student' | 'guest'
Key Facts
- โขTemplate literal types:
type Greeting = `Hello, ${string}`;โ prefix/suffix patterns - โข
as conston arrays produces a readonly tuple of literal types โ useful for enums - โขLiteral types are erased at runtime โ they're purely compile-time
- โขTypeScript uses literal types internally for HTML element names, CSS property values, event names
Watch Out!
When you assign a string literal to a let variable, TypeScript widens the type to string. To keep the literal: use const, or annotate explicitly: let dir: Direction = 'north', or use as const on the value.
Remember
'a' | 'b' | 'c' = exactly these values allowed. Use for status fields, variants, modes, directions โ anywhere the set of valid values is known and finite.
What You Learned
- โขLiteral types restrict values to specific strings/numbers/booleans โ not just the base type
- โขUnion of literals:
'idle' | 'loading' | 'error'โ TypeScript catches any other value - โขUnlocks: precise API contracts, exhaustive switch checks, self-documenting type definitions
Key Facts
- โTemplate literal types:
type Greeting = `Hello, ${string}`;โ prefix/suffix patterns - โ
as conston arrays produces a readonly tuple of literal types โ useful for enums - โLiteral types are erased at runtime โ they're purely compile-time
- โTypeScript uses literal types internally for HTML element names, CSS property values, event names
Real-World Examples
Remember
'a' | 'b' | 'c' = exactly these values allowed. Use for status fields, variants, modes, directions โ anywhere the set of valid values is known and finite.
Quick Quiz
Literal type 'on' | 'off' means?