Enums
In TypeScript, an enum
is a set of named constant values.
Enums can be defined in different ways:
Numeric enums
In TypeScript, a Numeric Enum is an Enum where each constant is assigned a numeric value, starting from 0 by default.
It is possible to specify custom values by explicitly assigning them:
String enums
In TypeScript, a String enum is an Enum where each constant is assigned a string value.
Note: TypeScript allows the usage of heterogeneous Enums where string and numeric members can coexist.
Constant enums
A constant enum in TypeScript is a special type of Enum where all the values are known at compile time and are inlined wherever the enum is used, resulting in more efficient code.
Will be compiled into:
Notes: Const Enums have hardcoded values, erasing the Enum, which can be more efficient in self-contained libraries but is generally not desirable. Also, Const enums cannot have computed members.
Reverse mapping
In TypeScript, reverse mappings in Enums refer to the ability to retrieve the Enum member name from its value. By default, Enum members have forward mappings from name to value, but reverse mappings can be created by explicitly setting values for each member. Reverse mappings are useful when you need to look up an Enum member by its value, or when you need to iterate over all the Enum members. Note that only numeric enums members will generate reverse mappings, while String Enum members do not get a reverse mapping generated at all.
The following enum:
Compiles to:
Therefore, mapping values to keys works for numeric enum members, but not for string enum members:
Ambient enums
An ambient enum in TypeScript is a type of Enum that is defined in a declaration file (*.d.ts) without an associated implementation. It allows you to define a set of named constants that can be used in a type-safe way across different files without having to import the implementation details in each file.
Computed and constant members
In TypeScript, a computed member is a member of an Enum that has a value calculated at runtime, while a constant member is a member whose value is set at compile-time and cannot be changed during runtime. Computed members are allowed in regular Enums, while constant members are allowed in both regular and const enums.
Enums are denoted by unions comprising their member types. The values of each member can be determined through constant or non-constant expressions, with members possessing constant values being assigned literal types. To illustrate, consider the declaration of type E and its subtypes E.A, E.B, and E.C. In this case, E represents the union E.A | E.B | E.C.