Updated March 28, 2023
Introduction to TypeScript enum
The new data type supported in TypeScript is called enumerations or enum using which lets us declare names which means a collection of related values which can be either string or numeric, and there are three types of enums in TypeScript, namely Numeric enum, string enum and Heterogeneous enum where numeric enum are the enums based on numbers that are they store numbers in the form of the string, string enum is the enums based on strings that is they store string values, heterogeneous enum are the enums based on both strings and numbers that is they store both string and numeric values.
The syntax to declare enum in TypeScript is as follows:
enum enum_name{
enum_value1,
enum_value2,
..
Enum_valuen
}
where enum_name represents the name of the enum,
enum_value1, enum_value2, and enum_valuen represent the collection of related values, either string or numeric.
Working of enum in TypeScript
- The new data type supported in TypeScript is called enumerations or enum using which lets us declare names which means a collection of related values that can be either string or numeric.
- The keyword enum is used to define enum in TypeScript.
- There are three types of enum in TypeScript, namely Numeric enum, string enum, and Heterogeneous enum.
- Numeric enum is the enums based on numbers; they store numbers in the form of strings.
- The enum values for numeric enum begin from zero and increase by one for each number.
- There is also an option to initialize the first enum value ourselves in a numeric enum, and the rest of the values would be increased by one.
- The enum values do not have to be in sequence in the numeric enum.
- String enum is the enums based on strings that is they store string values.
- The readability of the program increases by using a string enum.
- Heterogeneous enum is the enums based on both strings and numbers; they store both string and numeric values.
Examples of TypeScript enum
Different examples are mentioned below:
Example #1
TypeScript program to demonstrate the usage of numeric enum that initializes values and are displayed as the output on the screen:
Code:
//enum keyword is used to define a related collection of numeric values
enum Country
{
India,
USA,
Germany,
London
}
//displaying the values that are initialized by enum
console.log("The values initialized by enum is as follows:");
console.log(Country.India);
console.log(Country.USA);
console.log(Country.Germany);
console.log(Country.London);
Output:
In the above program, by using the keyword enum, we define a numeric enum whose values are initialized starting from zero and incremented by one for each member. Then the initialized values for the numeric enum are displayed as the output on the screen.
Example #2
TypeScript program to demonstrate the usage of numeric enum that initializes values where the first value is initialized by ourselves and are displayed as the output on the screen:
Code:
//enum keyword is used to define a related collection of numeric values in which the first value is initialized by ourselves as 5
enum Country
{
India = 5,
USA,
Germany,
London
}
//displaying the values that are initialized by enum
console.log("The values initialized by enum is as follows:");
console.log(Country.India);
console.log(Country.USA);
console.log(Country.Germany);
console.log(Country.London);
Output:
In the above program, by making use of the keyword enum, we are defining a numeric enum in which the first value is initialized as five by ourselves and then incremented by one for each member. Then the initialized values for the numeric enum are displayed as the output on the screen.
Example #3
TypeScript program to demonstrate the usage of string enum that initializes string values and are displayed as the output on the screen:
Code:
//enum keyword is used to define a related collection of string values
enum Capital
{
India = 'New Delhi',
USA = 'Washington',
Germany = 'Berlin',
England = 'London'
}
//displaying the values that are initialized by enum
console.log("The values initialized by string enum is as follows:");
console.log(Capital.India);
console.log(Capital.USA);
console.log(Capital.Germany);
console.log(Capital.England);
Output:
In the above program, by using the keyword enum, we define a string enum whose values are initialized to certain string values. Then the initialized values for the string enum is displayed as the output on the screen.
Example #4
TypeScript program to demonstrate the usage of heterogeneous enum that initializes both string values and numeric values and are displayed as the output on the screen:
Code:
//enum keyword is used to define a related collection of both string values and numeric values
enum Capital
{
India = 1,
USA = 'Washington',
Germany = 3,
England = 'London'
}
//displaying the values that are initialized by enum
console.log("The values initialized by heterogeneous enum is as follows:");
console.log(Capital.India);
console.log(Capital.USA);
console.log(Capital.Germany);
console.log(Capital.England);
Output:
In the above program, by using the keyword enum, we define a heterogeneous enum whose values are initialized to certain string values and certain numeric values. Then the initialized values for the heterogeneous enum is displayed as the output on the screen.
Example #5
TypeScript program to demonstrate the usage of heterogeneous enum that initializes both string values and numeric values and are displayed as the output on the screen:
Code:
//enum keyword is used to define a related collection of both string values and numeric values
enum Capital
{
India = 8,
USA = 12,
Germany = 'Berlin',
England = 'London'
}
//displaying the values that are initialized by enum
console.log("The values initialized by heterogeneous enum is as follows:");
console.log(Capital.India);
console.log(Capital.USA);
console.log(Capital.Germany);
console.log(Capital.England);
Output:
In the above program, by using the keyword enum, we define a heterogeneous enum whose values are initialized to certain string values and certain numeric values. Then the initialized values for the heterogeneous enum is displayed as the output on the screen.
Recommended Articles
We hope that this EDUCBA information on “TypeScript enum” was beneficial to you. You can view EDUCBA’s recommended articles for more information.