Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript.
Enums allow a developer to define a set of named constants.Using enums can make it easier to document intent, or create a set of distinct cases.TypeScript provides both numeric and string-based enums.
Numeric enumsWe’ll first start off with numeric enums, which are probably more familiar if you’re coming from other languages.An enum can be defined using the enum keyword.
tsenum Direction { Up = 1, Down, Left, Right,}TryAbove, we have a numeric enum where Up is initialized with 1.All of the following members are auto-incremented from that point on.In other words, Direction.Up has the value 1, Down has 2, Left has 3, and Right has 4.
If we wanted, we could leave off the initializers entirely:
tsenum Direction { Up, Down, Left, Right,}TryHere, Up would have the value 0, Down would have 1, etc.This auto-incrementing behavior is useful for cases where we might not care about the member values themselves, but do care that each value is distinct from other values in the same enum.
Using an enum is simple: just access any member as a property off of the enum itself, and declare types using the name of the enum:
tsenum UserResponse { No = 0, Yes = 1,} function respond(recipient: string, message: UserResponse): void { // ...} respond("Princess Caroline", UserResponse.Yes);TryNumeric enums can be mixed in computed and constant members (see below).The short story is, enums without initializers either need to be first, or have to come after numeric enums initialized with numeric constants or other constant enum members.In other words, the following isn’t allowed:
tsenum E { A = getSomeValue(), B,Enum member must have initializer.1061Enum member must have initializer.}TryString enumsString enums are a similar concept, but have some subtle runtime differences as documented below.In a string enum, each member has to be constant-initialized with a string literal, or with another string enum member.
tsenum Direction { Up = "UP", Down = "DOWN", Left = "LEFT", Right = "RIGHT",}TryWhile string enums don’t have auto-incrementing behavior, string enums have the benefit that they “serialize” well.In other words, if you were debugging and had to read the runtime value of a numeric enum, the value is often opaque - it doesn’t convey any useful meaning on its own (though reverse mapping can often help). String enums allow you to give a meaningful and readable value when your code runs, independent of the name of the enum member itself.
Heterogeneous enumsTechnically enums can be mixed with string and numeric members, but it’s not clear why you would ever want to do so:
tsenum BooleanLikeHeterogeneousEnum { No = 0, Yes = "YES",}TryUnless you’re really trying to take advantage of JavaScript’s runtime behavior in a clever way, it’s advised that you don’t do this.
Computed and constant membersEach enum member has a value associated with it which can be either constant or computed.An enum member is considered constant if:
It is the first member in the enum and it has no initializer, in which case it’s assigned the value 0:
ts// E.X is constant:enum E { X,}TryIt does not have an initializer and the preceding enum member was a numeric constant.In this case the value of the current enum member will be the value of the preceding enum member plus one.
ts// All enum members in 'E1' and 'E2' are constant. enum E1 { X, Y, Z,} enum E2 { A = 1, B, C,}TryThe enum member is initialized with a constant enum expression.A constant enum expression is a subset of TypeScript expressions that can be fully evaluated at compile time.An expression is a constant enum expression if it is:
a literal enum expression (basically a string literal or a numeric literal)a reference to previously defined constant enum member (which can originate from a different enum)a parenthesized constant enum expressionone of the +, -, ~ unary operators applied to constant enum expression+, -, *, /, %, , >>>, &, |, ^ binary operators with constant enum expressions as operandsIt is a compile time error for constant enum expressions to be evaluated to NaN or Infinity.
In all other cases enum member is considered computed.
tsenum FileAccess { // constant members None, Read = 1