๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
๐Ÿ“˜ TypeScript

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โ€ฆ

8 min 10 XP Lesson 7 of 21
Literal types
๐ŸŒ

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 to string unless 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 const on 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 const on 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

โ€ข HTTP methods: <code>'GET' | 'POST' | 'PUT' | 'DELETE'</code> โ€ข Notification types: <code>'success' | 'warning' | 'error' | 'info'</code> โ€ข Button variants: <code>'primary' | 'secondary' | 'ghost' | 'destructive'</code> โ€ข User roles: <code>'admin' | 'teacher' | 'student' | 'guest'</code>

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

1 / 2

Literal type 'on' | 'off' means?