Typescript
Menu
Enums
- Enums in TypeScript are a powerful feature that allows you to define a set of named constants, making your code more readable and maintainable.
- They can represent a group of related values, such as states, directions, or categories.
- Below are the key aspects of enums in TypeScript, explained in bullet points with coding examples.
Definition of Enums
- Basic Syntax: Enums are defined using the enum keyword followed by the name of the enum and its members enclosed in curly braces.
enum Direction {
Up,
Down,
Left,
Right
}
- Basic Syntax: Enums are defined using the enum keyword followed by the name of the enum and its members enclosed in curly braces.
Numeric Enums
- Default Numeric Enums: By default, enums start at 0 and increment by 1 for each subsequent member.
enum CardinalDirections {
North, // 0
East, // 1
South, // 2
West // 3
} - Initialized Numeric Enums: You can specify the starting value for an enum.
enum CardinalDirections {
North = 1, // Starts from 1
East, // 2
South, // 3
West // 4
} - Fully Initialized Numeric Enums: You can assign specific values to each member.
enum StatusCodes {
NotFound = 404,
Success = 200,
Accepted = 202,
BadRequest = 400
}
console.log(StatusCodes.NotFound); // Outputs: 404
- Default Numeric Enums: By default, enums start at 0 and increment by 1 for each subsequent member.
String Enums
- String enums allow you to define enums with string values instead of numeric values. This is often preferred for readability.
enum CardinalDirections {
North = "North",
East = "East",
South = "South",
West = "West"
}
console.log(CardinalDirections.North); // Outputs: "North"
- String enums allow you to define enums with string values instead of numeric values. This is often preferred for readability.
Heterogeneous Enums
- Mixing Types: You can mix string and numeric values in an enum, although this is generally not recommended for clarity.
enum MixedEnum {
No = "No",
Yes = "Yes",
Maybe = 2 // Numeric value mixed with strings
}
- Mixing Types: You can mix string and numeric values in an enum, although this is generally not recommended for clarity.
Reverse Mapping:
- Accessing Enum Members: Numeric enums support reverse mapping, which allows you to get the name of an enum member from its value.
enum CardinalDirections {
North = 1,
East,
South,
West
}
console.log(CardinalDirections[1]); // Outputs: "North" - Limitations with String Enums: String enums do not support reverse mapping because their values are not unique.
enum CardinalDirections {
North = "North",
East = "East"
}
// console.log(CardinalDirections["North"]); // Error: Cannot access string enums this way.
- Accessing Enum Members: Numeric enums support reverse mapping, which allows you to get the name of an enum member from its value.
Computed and Constant Members
- Constant Members: These are initialized with constant expressions that can be evaluated at compile time.
const constantValue = 10;
enum ExampleEnum {
A = constantValue, // Error: computed value must be constant.
B = constantValue + 5 // Error: computed value must be constant.
}
- Constant Members: These are initialized with constant expressions that can be evaluated at compile time.
Ambient Enums
- Ambient Enums: These are used to describe the shape of an existing enum that is defined elsewhere (eg in a library).
declare enum AmbientEnum {
A,
B,
C
}
- Ambient Enums: These are used to describe the shape of an existing enum that is defined elsewhere (eg in a library).
Conclusion
- Enums in TypeScript provide a way to create named constants that improve code clarity and maintainability.
- By understanding how to effectively define and use numeric and string enumerations, you can more effectively manage sets of related values in your TypeScript applications.
- Whether you choose numeric or string enums depends on your specific use case and the readability requirements of your code.