Enums
Enums (short for "enumerations") are a set of named constants. They make your code more readable by replacing magic numbers or strings with meaningful names, and can be used as both a type and a value.
Loading TypeScript...
Each value in an enum can be associated with a unique number or string.
Loading TypeScript...
Consider the example below, where enum values are dynamically constructed in the getEvent
function.
Loading TypeScript...
Notice that the as
keyword allowed us to construct an enum value "spread_bread"
which is not in the original enum. As you can see, the responsibility of asserting types correctly inevitably falls to the programmer.