TypeScriptTypeScript15 min read

TypeScript Literal Types

Learn literal types to restrict values to exact strings or numbers, useful for configs and API states.

David Miller
Nov 18, 2025
6,387217

Literal types mean: value must be exact.

  ## Example
  
  ```ts
  let status: "loading" | "success" | "error";
  
  status = "loading"; // ok
  // status = "done"; ❌ error
  ```
  
  ## Why useful?
  Prevents invalid states.
  
  ```ts
  type Role = "admin" | "user";
  
  function setRole(role: Role) {
    console.log(role);
  }
  ```
  
  ## With numbers
  
  ```ts
  type Dice = 1 | 2 | 3 | 4 | 5 | 6;
  ```
  
  ## Graph
  
  ```mermaid
  flowchart LR
    A[status] --> B[loading]
    A --> C[success]
    A --> D[error]
  ```
  
  ## Remember
  - Literal types lock values
  - Great for states and configs
  
#TypeScript#Beginner#Types