TypeScript Enums

Enums in TypeScript are used to define a set of named constants, improving code readability and maintainability. They provide a way to group related values with meaningful names, making the code more expressive.

 

Why Use Enums?

  • Group related constants under a single name.
  • Improve code readability by replacing magic numbers or strings.
  • Enums can be numeric or string-based.

 

Numeric Enums

By default, TypeScript assigns numeric values starting from 0 to each member of the enum.

enum Direction {  
  Up,  
  Down,  
  Left,  
  Right  
}  

console.log(Direction.Up); // Output: 0  
console.log(Direction.Down); // Output: 1  
console.log(Direction.Left); // Output: 2  
console.log(Direction.Right); // Output: 3

You can explicitly assign values to enum members:

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

console.log(Direction.Up); // Output: 10  
console.log(Direction.Down); // Output: 11  
console.log(Direction.Left); // Output: 12  
console.log(Direction.Right); // Output: 13

String Enums

In string enums, each member is assigned a string value instead of a number.

enum Status {  
  Success = "SUCCESS",  
  Failure = "FAILURE",  
  Pending = "PENDING"  
}  

console.log(Status.Success); // Output: "SUCCESS"  
console.log(Status.Failure); // Output: "FAILURE"

Heterogeneous Enums

Enums can have a mix of string and numeric values, although this is less common.

enum MixedEnum {  
  Success = "SUCCESS",  
  Error = 500,  
  Timeout = 408  
}  

console.log(MixedEnum.Success); // Output: "SUCCESS"  
console.log(MixedEnum.Error); // Output: 500  

Reverse Mapping

For numeric enums, TypeScript provides reverse mapping, meaning you can get the name of an enum member from its value.

enum Color {  
  Red = 1,  
  Green,  
  Blue  
}  

console.log(Color.Green); // Output: 2  
console.log(Color[2]);    // Output: "Green"

 

Note: Reverse mapping does not work for string enums.

 

Const Enums

If you want to optimize enums for performance, use const enum. It removes enum objects at runtime and directly inlines values where they are used.

const enum Size {  
  Small = 1,  
  Medium = 2,  
  Large = 3  
}  

console.log(Size.Small); // Output: 1  

Enum with Functions

Enums can be used with functions to improve clarity.

enum Days {  
  Monday,  
  Tuesday,  
  Wednesday,  
  Thursday,  
  Friday  
}  

function isWorkingDay(day: Days): boolean {  
  return day !== Days.Saturday && day !== Days.Sunday;  
}  

console.log(isWorkingDay(Days.Monday)); // Output: true  

Enums as Object Keys

Enums can also be used as object keys for better organization.

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

const permissions = {  
  [UserRole.Admin]: "Full Access",  
  [UserRole.User]: "Limited Access"  
};  

console.log(permissions[UserRole.Admin]); // Output: Full Access

 

Summary

Enums in TypeScript provide a structured way to define a group of related constants. Numeric, string, and heterogeneous enums allow flexibility, while const enum optimizes performance. Enums are particularly useful when dealing with fixed sets of values, improving code clarity and maintainability.