TypeScript Enums
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.
enum Direction {
Up,
Down,
Left,
Right
}
let move: Direction = Direction.Up;
console.log(move);
- First member is 0: Direction.Up is 0.
- Auto-increment: Direction.Down is 1, Direction.Left is 2, and so on.
Output:
0
Initialized Numeric Enums:
You can assign a specific value to the first member, and subsequent members will auto-increment from that value.
enum Direction {
Up = 1,
Down,
Left,
Right
}
let move: Direction = Direction.Up;
console.log(move);
- Custom start: Direction.Up is set to 1.
- Auto-increment continues: Direction.Down becomes 2, Direction.Left becomes 3, etc.
Output:
1
Fully Initialized Numeric Enums:
Each member can be assigned a unique numeric value, independent of its position.
enum Direction {
Up = 1,
Down = 3,
Left = 5,
Right = 7
}
let move: Direction = Direction.Up;
console.log(move);
- 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.
Output:
1
2. String Enums
String enums allow you to assign string values to each member, providing meaningful names that enhance code clarity.
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT"
}
let move: Direction = Direction.Up;
console.log(move);
- Each member is assigned a descriptive string value.
- Logging Direction.Up outputs "UP".
Output:
"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.
enum Status {
Active = 1,
Inactive = "INACTIVE",
Pending = 2,
Cancelled = "CANCELLED"
}
let currentStatus: Status = Status.Active;
console.log(currentStatus); // Output: 1
let cancelledStatus: Status = Status.Cancelled;
console.log(cancelledStatus); // Output: "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.
Best Practices of Using TypeScript Enums
- Use clear and descriptive names: Give enum members meaningful names to make your code easier to read and understand.
- Prefer const enums for performance: Use const enums in performance-critical code to reduce runtime overhead.
- Avoid mixing numbers and strings: Stick to either numeric or string enums for consistency and simplicity.
- Use string enums for better debugging: String enums provide meaningful values that make debugging easier.
TypeScript Enums - FAQs
What is an enum in TypeScript?
An enum (enumeration) is a way to define a set of named constants, making your code more readable and organized by grouping related values under one name.
What are the downsides of enums in TypeScript?
- Runtime overhead: Regular enums generate extra JavaScript code, which can impact performance.
- Complexity: Mixing string and numeric values in enums can make code harder to maintain.
What are the two main types of enums?
- Numeric Enums: Members are assigned numeric values (default starts at 0).
- String Enums: Members are assigned string values for better readability.
What are const enums in TypeScript?
const enums are enums optimized for performance by inlining their values at compile time.
Are enums mandatory in TypeScript?
No, but they are helpful for managing related constants and improving code structure.