Enums in TypeScript: Defining Named Constants

Enums in TypeScript: Defining Named Constants

Organize related values with enum types

Author
Nguyen Bao Huy
03:39:00 22/02/2026
2 min read
0 comments

TypeScript Enums allows you to create a list of constants (unchanging variables) and give them easy-to-remember names. These names make your code easier to read and understand by grouping related values together under one name. Enums can use both numbers and text, so you can choose what works best for your code.

  • Easy-to-remember names: Replace hard-to-understand numbers or strings with meaningful names.
  • Group-related values: Keep similar values organized under a single identifier for better code structure.

Types of Enums in TypeScript

TypeScript provides two main types of enums: Numeric and String enums.

1. Numeric Enums

Numeric enums are the default in TypeScript. Each member of a numeric enum is assigned a numeric value, starting from 0 by default, but you can customize these values as needed.

Default Numeric Enums: In default numeric enums, the first member is assigned the value 0, and each subsequent member is incremented by 1.

typescript
enum Direction {
    Up,
    Down,
    Left,
    Right
}

let move: Direction = Direction.Up;
console.log(move);

Output:

javascript
0
  • First member is 0: Direction.Up is 0.
  • Auto-increment: Direction.Down is 1, Direction.Left is 2, and so on.

Initialized Numeric Enums: You can assign a specific value to the first member, and subsequent members will auto-increment from that value.

typescript
enum Direction {
    Up = 1,
    Down,
    Left,
    Right
}

let move: Direction = Direction.Up;
console.log(move);

Output:

javascript
1
  • Custom start: Direction.Up is set to 1.
  • Auto-increment continues: Direction.Down becomes 2, Direction.Left becomes 3, etc.

Fully Initialized Numeric Enums:

Each member can be assigned a unique numeric value, independent of its position.

typescript
enum Direction {
    Up = 1,
    Down = 3,
    Left = 5,
    Right = 7
}

let move: Direction = Direction.Up;
console.log(move);

Output:

javascript
1
  • Each member has a specific value: Up is 1, Down is 3, Left is 5, and Right is 7.
  • Logging Direction.Up outputs its assigned value, 1.

2. String Enums

String enums allow you to assign string values to each member, providing meaningful names that enhance code clarity.

typescript
enum Direction {
    Up = "UP",
    Down = "DOWN",
    Left = "LEFT",
    Right = "RIGHT"
}

let move: Direction = Direction.Up;
console.log(move);

Output:

javascript
"UP"
  • Each member is assigned a descriptive string value.
  • Logging Direction.Up outputs "UP".

3. Heterogeneous Enums

TypeScript also supports heterogeneous enums, where you can mix both numeric and string values in the same enum. However, this is not commonly used because it can make the code less consistent and harder to maintain.

typescript
enum Status {
    Active = 1,          
    Inactive = "INACTIVE", 
    Pending = 2,         
    Cancelled = "CANCELLED" 
}

let currentStatus: Status = Status.Active;
console.log(currentStatus); 

let cancelledStatus: Status = Status.Cancelled;
console.log(cancelledStatus);

Output:

javascript
1
CANCELLED
  • Mix of numbers and strings: Members can have either numeric or string values.
  • Less common: Not recommended for most cases as it reduces consistency.
Author avatar

Nguyen Bao Huy

Lead Fullstack & AI Solutions Engineer

Specializing in Next.js App Router, React 19, TypeScript, and modern design systems. Passionate about creating seamless user experiences.

Discussion

0

No comments yet

Be the first to share your thoughts, question a concept, or provide additional tips!

Leave a Reply

Share your insights, questions, or solutions with the developer community.

Your avatar
0 / 500 characters