What is an enumeration in TypeScript?
The syntax of an enumeration is as follows.
What to look for when using enumerations in TypeScript
Types of common enumerations
Numeric enumerations and String enumerations are the most common types of enumerations we use in TypeScript, and I’ll introduce you to their features and how to use them with examples.
Numeric enumerations store numeric values as strings. Let’s define them using the enum keyword. I’ll show the numeric enumeration in TypeScript with an example that stores a set of different types of cars.
The enumeration value CarType has four values: Honda, Toyota, Subaru and Hyundai. The enumerated values start at 0 and increment by 1 for each member, as follows.
Honda = 0
Toyota = 1
Subaru = 2
Hyundai = 3
You can initialize the first value yourself if needed, as follows.
In the example above, Honda initialized the first member with the value 1. The remaining numbers will be added by one.
You may wonder, if I change the last value, will the previous values be decremented according to the last defined value? Let’s try.
Unfortunately this does not work and the current example has the following values
Note: It is not necessary to assign sequential values to enumeration members. You can assign any desired value to it
String enumerations are similar to numeric enumerations, but their enumeration values are initialized using string values instead of numeric values. String enumerations have better readability than numeric enumerations, making it easier to debug programs.
The following example uses the same information as the numeric enumeration example, but is expressed as a string enumeration.
Note: String enumeration values need to be initialized separately.
Enumeration reverse mapping
An enumeration can retrieve the num value using its corresponding enumeration member value. Using the reverse mapping, the member value and the name of the member value can be accessed, see the following example.
CarType returns its member name " Subaru" due to the reverse mapping. Let us look at another example.
The following output will be seen in the browser’s console.
Each value of the enumeration appears twice in the internally stored enumeration object.
The value of an enumeration member can be a constant value or a calculated value. See the following example.
If the enumeration contains both computed and constant members, the uninitialized enumeration member will appear first and may also come after other initialized members with numeric constants. The next example will show the error.
You can declare the above enumeration like this.
That’s all this article is about, by explaining what is enumeration and what we should pay attention to when using enumeration. To our common enumeration types (numeric enumeration, string enumeration), enumeration reverse mapping, computational enumeration. I believe you have a certain understanding of the enumeration, if there is anything that is not clear or what is wrong with the article welcome to correct, thank you.