Enum
defines a set of named constants. In TypeScript, the value of the enum can be numeric or string.
enums
The code below demonstrates how to create numeric enum
.
enum Direction {Up ,Down,Left = 8,Right,}console.log("Direction.Up :", Direction.Up);console.log("Direction.Down :", Direction.Down);console.log("Direction.Left :", Direction.Left);console.log("Direction.Right :", Direction.Right);console.log("\nThe Direction enum is \n", Direction);console.log("\nReverse mapping in typescript ")console.log(`Direction[${Direction.Up}] :`, Direction[Direction.Up]);console.log(`Direction[${Direction.Down}] :`, Direction[Direction.Down]);console.log(`Direction[${Direction.Left}] :`, Direction[Direction.Left]);console.log(`Direction[${Direction.Right}] :`, Direction[Direction.Right]);
Direction
with four members Up
, Down
, Left
, Right
. We didn't give an initial value for the first member, Up
. TypeScript will initialize with 0
. The following members will have auto-incremented values from the previous member. We have set the 8
as value for the member Left
so the value of the next member Right
will be 9.
Direction
enum.Direction
enum. On console, we'll notice that the enum contains additional members. This is due to the reverse mapping process of TypeScript. With this, we can access the value of the member using its name and the name of the member from its value.enums
It is similar to numeric enums but instead of the numeric value, it will have a string literal or another string enum
member.
enum Greeting {HI = "hi",HELLO = "hello",}console.log("\nThe Greeting enum is \n", Greeting);
In the code above, We create an enum
called Greeting
with two members, HI
and HELLO
. These members have the values, "hi"
and "hello"
respectively.
Note: String
enum
values need to be individually initialized, otherwise, we'll get compilation error. Also, the reverse mapping is not applied for String enums.
enums
Heterogeneous enums
contain both numeric and string values.
enum ERROR_CODE{A = 10,FILE_NOT_FOUND = "404",}console.log(ERROR_CODE)
In the code above, we create an enum
called ERROR_CODE
with two members, A
and FILE_NOT_FOUND
. For the member, A
, we assign the numeric value, 10
. For the member, FILE_NOT_FOUND
, we assign the string value, "404"
.
enums
We can define a constant enum
, which can improve the performance by removing the extra generated code and additional indirection when accessing enum
values.
const enum ConstEnum {A = 1,B = 10,}console.log(ConstEnum.A)console.log(ConstEnum.B)
In the code above, We create a constant enum
called ConstEnum
with two members, A
and B
, with values 1 and 10 respectively. During the compilation of the above code the, ConstEnum.A
and ConstEnum.B
will be replaced with the value 1 and 10 respectively.
enums
The values of the enum can be a constant or a computed value.
enum ComputedEnum {TEN = getTen(),TWENTY = TEN + 10,}function getTen(): number{return 10;}console.log(ComputedEnum)
In the code above, we create a constant enum
called ComputedEnum
with two members, TEN
and TWENTY
. We create a function getTen
that will return 10
. We assign the value returned by calling the getTen()
method as the value for the member, TEN
. For the member TWENTY
, the value will be TEN + 10
.
Note: A complete guide to
enums
is present here and here.