Enums
A set of named constants, with numeric and string variants and why many teams reach for literal unions instead.
Numeric enums
By default, enum members are assigned increasing numbers starting at 0:
You can set a starting value, and the rest increment from there:
String enums
Each member must be initialized with a string literal. No auto-incrementing, and no reverse lookup but values are more readable in logs and network requests:
const enums
Fully inlined at compile time no actual object is generated in the compiled JS, which makes them slightly more efficient, at the cost of some flexibility (they can't be used in every situation a regular enum can, like iterating over its members):
Why many teams prefer literal unions
Enums generate real runtime code and have a few quirks (numeric enums allow any number to be assigned, not just the declared members). A plain union of string literals often gives the same safety with none of the runtime cost:
Rule of thumb: reach for a literal union by default; reach for an enum when you specifically want a real runtime object (e.g. to iterate over all values, or because the values need to sync with a backend that already uses enums).