Enum in TypeScript

Enumeration (or enum) allow us to define a set of named constants. TypeScript supports 3 types of enums as well as flag based enum:

  1. Numeric
  2. String
  3. Heterogeneous enum

Numeric Enum

In the above mentioned enum , an integer value is auto assigned to each of the enum item.

Red =0 , Green = 1, Blue = 2.

If needed, values can be assigned to each enum item.

Return enum from function:

String Enum

Heterogeneous Enum

Reverse Mapping of Enum

Numeric enums members get a reverse mapping from enum values to enum names.

Number Enum as flags

TypeScript enum can be used as flag based enum. Flags allows to check if a certain condition from a set of conditions is true.

The bitwise operators | (or),  & (and) , / ~ (not) are used to manipulate flag based enums.