TYPESCRIPT

TypeScript Enum: Syntax, Usage, and Examples

A TypeScript enum defines a set of named constants, making code more readable and maintainable. Enums allow you to assign meaningful names to numeric or string values, reducing the likelihood of using arbitrary numbers or strings in your code.

What Is an Enum in TypeScript?

An enum in TypeScript represents a collection of related values that remain constant. Instead of using hardcoded numbers or strings, enums provide a way to define a set of meaningful identifiers.

enum Status {
  Pending,
  InProgress,
  Completed,
}

let taskStatus: Status = Status.InProgress;
console.log(taskStatus); // Output: 1

TypeScript assigns numeric values starting from 0 unless specified otherwise.

Enum in TypeScript vs. Union Types

Enums and union types provide similar functionality but work differently. An enum generates JavaScript code, while a union type exists only at compile time.

Using an enum:

enum Role {
  Admin = "ADMIN",
  User = "USER",
}

function isAdmin(role: Role) {
  return role === Role.Admin;
}

Using a union type:

type RoleUnion = "ADMIN" | "USER";

function isAdminUnion(role: RoleUnion) {
  return role === "ADMIN";
}

Union types are lighter because they don’t create additional JavaScript code. Enums, however, provide auto-incremented values and better debugging support.

How to Use TypeScript Enums

Define a Numeric Enum

A numeric enum is the default type in TypeScript.

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

console.log(Direction.Up); // Output: 1
console.log(Direction.Right); // Output: 4

TypeScript increments the values automatically when you assign an initial value.

Define a String Enum

String enums use string values instead of numbers.

enum Response {
  Success = "SUCCESS",
  Failure = "FAILURE",
}

console.log(Response.Success); // Output: "SUCCESS"

String enums are useful when debugging since the logged values are human-readable.

Make an Enum from an Existing Enum in TypeScript

You can create an enum by extending another enum. TypeScript does not support direct inheritance of enums, but you can combine them manually.

enum BaseEnum {
  First = "FIRST",
  Second = "SECOND",
}

enum ExtendedEnum {
  ...BaseEnum,
  Third = "THIRD",
}

console.log(ExtendedEnum.First); // Output: "FIRST"

TypeScript Enum to String

Use indexing to convert an enum to a string.

enum Fruits {
  Apple = "APPLE",
  Banana = "BANANA",
}

console.log(Fruits.Apple); // Output: "APPLE"

TypeScript String to Enum

You can convert a string to an enum value by accessing the key dynamically.

enum Category {
  Books = "BOOKS",
  Electronics = "ELECTRONICS",
}

let category: Category = Category["Books"];
console.log(category); // Output: "BOOKS"

TypeScript Define Type as Enum

Instead of an enum, you can define a type with literal values.

type Priority = "High" | "Medium" | "Low";

let taskPriority: Priority = "High";

This approach provides better type inference and avoids generating extra JavaScript code.

When Should You Use TypeScript Enums?

Improve Code Readability

Enums replace magic numbers or arbitrary strings, making the code easier to understand.

// Without Enums
let status = "completed";

// With Enums
enum TaskStatus {
  Pending,
  InProgress,
  Completed,
}

let task: TaskStatus = TaskStatus.Completed;

Ensure Consistency Across Code

When multiple functions rely on the same set of values, enums prevent typos or mismatches.

enum Roles {
  Admin = "ADMIN",
  User = "USER",
  Guest = "GUEST",
}

function getPermissions(role: Roles) {
  if (role === Roles.Admin) {
    return "Full access";
  } else if (role === Roles.User) {
    return "Limited access";
  }
  return "Read-only access";
}

Iterate Over Enum Values

To iterate over an enum, use Object.values() or Object.keys():

enum Days {
  Monday,
  Tuesday,
  Wednesday,
}

Object.values(Days).forEach(day => console.log(day));
// Output: 0, 1, 2, "Monday", "Tuesday", "Wednesday"

Examples of TypeScript Enums

Convert an Enum to an Array

You can create an array from an enum's values.

enum Sizes {
  Small = "S",
  Medium = "M",
  Large = "L",
}

const sizeArray: string[] = Object.values(Sizes);
console.log(sizeArray); // Output: ["S", "M", "L"]

Extend an Enum

You can't directly extend an enum, but you can merge enums using an intersection type.

enum BasicPermissions {
  Read = "READ",
  Write = "WRITE",
}

enum AdminPermissions {
  Delete = "DELETE",
}

type AllPermissions = BasicPermissions | AdminPermissions;

const permission: AllPermissions = BasicPermissions.Read;
console.log(permission); // Output: "READ"

TypeScript Iterate Over Enum

To loop through an enum, use Object.values():

enum Status {
  Open = "OPEN",
  Closed = "CLOSED",
}

Object.values(Status).forEach(status => console.log(status));

This is useful when displaying all possible enum values.

TypeScript Enum to Array

Convert an enum into an array using Object.values():

enum Colors {
  Red = "RED",
  Blue = "BLUE",
}

const colorArray = Object.values(Colors);
console.log(colorArray); // Output: ["RED", "BLUE"]

TypeScript Extend Enum

Enums don’t support inheritance, but you can simulate it:

enum BaseEnum {
  A = "A",
  B = "B",
}

enum ExtendedEnum {
  ...BaseEnum,
  C = "C",
}

console.log(ExtendedEnum.A); // Output: "A"

TypeScript Get Enum Value

You can retrieve an enum value dynamically:

enum OrderStatus {
  Processing,
  Shipped,
}

console.log(OrderStatus[0]); // Output: "Processing"

TypeScript Enum Type

An enum type represents a collection of fixed values. You can declare variables using enum types:

enum Severity {
  Low,
  Medium,
  High,
}

let issueSeverity: Severity = Severity.High;

TypeScript String Enum Tools

You can use built-in TypeScript utilities to manipulate enums:

enum Tools {
  Hammer = "HAMMER",
  Wrench = "WRENCH",
}

type ToolNames = keyof typeof Tools;
const tool: ToolNames = "Hammer";

This approach is useful for working with dynamically generated values.

TypeScript Get Enum Key from Value

You can find the key that matches an enum value:

enum Permissions {
  Read = "READ",
  Write = "WRITE",
}

const key = Object.keys(Permissions).find(k => Permissions[k as keyof typeof Permissions] === "READ");
console.log(key); // Output: "Read"

TypeScript enums help organize constant values and improve readability. Use enums when defining a fixed set of related values, especially when numeric or string-based constants are required.

Learn TypeScript for Free
Start learning now
button icon
To advance beyond this tutorial and learn TypeScript by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH