TypeScriptTypeScript16 min read

TypeScript Enums

Use enums to define named constants and make your code more readable and less error-prone.

David Miller
December 8, 2025
2.5k96

Enums group related constant values.

  ## Why enums?
  Avoid magic strings/numbers.
  
  ## Numeric enum
  
  ```ts
  enum Direction {
    Up,
    Down,
    Left,
    Right
  }
  
  let move: Direction = Direction.Up;
  ```
  
  ## String enum
  
  ```ts
  enum Status {
    Loading = "loading",
    Success = "success",
    Error = "error"
  }
  ```
  
  ## Using enum
  
  ```ts
  function setStatus(s: Status) {
    console.log(s);
  }
  
  setStatus(Status.Success);
  ```
  
  ## Enum vs union literals
  Union is lighter, enum is structured.
  Choose based on style and needs.
  
  ## Graph
  
  ```mermaid
  flowchart TD
    A[Status] --> B[Loading]
    A --> C[Success]
    A --> D[Error]
  ```
  
  ## Remember
  - Enums represent fixed options
  - Improve readability
  
#TypeScript#Beginner#Enums