Demystifying and mastering enums in TypeScript
Enums, also called Enumerations, are constants outlined that can be utilized wherever within the code. They’re comparatively simple to grasp.
Enums might be outlined in Typescript utilizing the enum
key phrase. That is how we will outline one:
enum Languages
English,
Spanish,
French,
German
By default, every of those might be assigned a price. So English might be 0, Spanish is 1, and so forth. With a view to use these values in code, we will simply check with it by the <EnumName.PropertyName>:
// Earlier than enum
console.log(1); // 1// After enum
console.log(Languages.Spanish); // 1
This fashion, we will add context to what we’re doing, through the use of our enum record.
It isn’t obligatory to make use of enums. However they supply semantics to your code. You simply want to recollect a key level that while you want a predefined record of values that symbolize some form of knowledge, which might be numbers or strings, you must use an enum. Through the use of so, it provides extra context to what we’re doing. It thus helps peer builders to grasp the code rapidly once they undergo it.
Enums don’t all the time begin with 0. We will outline enums as numbers throughout initialization. Let’s take a look at these in a little bit element.
enum Languages
English = 3,
Spanish,
French,
German
Above, now we have given English a numeric worth of three. Each merchandise after that might be incremented by 1, so Spanish turns into 4, French turns into 5, and so on,
Additionally, you may also simply outline them as you would like:
enum Languages
English = 3,
Spanish = 10,
French = 23,
German = 33
Often, we don’t combine strings and numbers in Enums to keep away from confusion. However, we will additionally outline enums as totally strings too:
enum TimeZones
India = "IST",
UK = "GMT",
Ukraine = "EET"
console.log(TimeZones.India) // IST
We will do extra with enums! They may also be the values of a perform. However there’s a catch. In case you solely outline one worth as a perform in an enum, capabilities should go on the finish for all of the values. In any other case, it should throw an error.
const getTimezoneForIndia = perform()
return 'IST';
enum TimeZones
India = getTimezoneForIndia(),
UK,
Ukraine,
// Error - UK, Ukraine will return undefined
The explanation why get an error is that TypeScript expects each property to be a price of a perform since we used it for the primary entry. Right here’s one other case. Whenever you outline a perform for the final Enum, we gained’t get an error:
const getTimezoneForUkraine = perform()
return 'EET';
enum TimeZones
India,
UK,
Ukraine = getTimezoneForUkraine(),
Within the above enum, India returns 0, UK returns 1, and Ukraine returns the worth of getTimezoneForUkraine()
.
Right here’s one other fascinating use case. Enums may also be calculated, (i.e. any arithmetic operation can be utilized as an enum’s worth):
enum Languages
English = 1 + 1, // Returns "2"
Spanish = 1 * 5, // Returns "5"
French // Returns "6"
You too can check with different enum’s values, or check with the worth of the identical enum. Right here’s how you are able to do it:
enum IndianLanguages
Hindi: Languages.English, // returns Languages.English, i.e. "2"
Tamil: 1 + 5, // Calculates and returns "6"
Malayalam: 1 * 5, // Calculates and returns "5"
Telugu: Hindi // Refers to AnotherEnum.One, returns "2"
In TypeScript, Enums are a really highly effective approach so as to add semantics to your code-base. It helps the peer builders to grasp your code simply and get to know what it does. It additionally helps within the maintainability and helps to scale, in case there are another values added sooner or later. I hope this text was helpful.
Comfortable coding!