Search⌘ K

Enum With and Without Values

Explore TypeScript enums and learn how to use them with or without assigned values. Understand enum value types, explicit and implicit assignments, bitwise operations, and practical use cases to enhance your coding precision and type control.

The role of enum #

An enum is a structure that proposes several allowed values for a variable. It is a way to constrain variable values by defining specific possible entries.

enum with values

enum can be of string type. In that case, every member requires a value without exception.

TypeScript 3.3.4
enum MyStringEnum {
ChoiceA = "A",
ChoiceB = "B",
}

A mixed enum value type is acceptable if every member is defined. For example, you can have one item be an integer and another be a string type. It is recommended not to mix types since it might be more confusing than pragmatic.

TypeScript 3.3.4
enum MyStringAndNumberEnum {
ChoiceA, // 0
ChoiceB = "B",
ChoiceC = 100
}

enum without values

enum is a type that enforces a limited and defined group of constants. enum must have a name and accepted ...