DEV Community

MOHAMED BELLA
MOHAMED BELLA

Posted on

Enums

Enums (short for enumerations) are a feature in TypeScript that allows you to define a set of named constants — values that are logically grouped together.

Instead of using strings or numbers directly throughout your code, Enums help make your code more readable, type-safe, and organized.

There are two main types:

Numeric Enums (default)

String Enums

Example:

enum Direction {
  North,
  South,
  East,
  West,
}
Enter fullscreen mode Exit fullscreen mode
function move(direction: Direction) {
  console.log("Moving", direction);
}
Enter fullscreen mode Exit fullscreen mode

Enums can also be assigned specific values:

enum Role {
  Admin = 'ADMIN',
  User = 'USER',
  Guest = 'GUEST'
}
Enter fullscreen mode Exit fullscreen mode

Now your logic can depend on a clear, strict set of roles or directions.

Top comments (0)