How Do Enums Work in TypeScript?. Demystifying and mastering enums in… | by Harsha Vardhan | Mar, 2022

Photograph by Deon Black on Unsplash
enum Languages 
English,
Spanish,
French,
German
// Earlier than enum
console.log(1); // 1
// After enum
console.log(Languages.Spanish); // 1
enum Languages 
English = 3,
Spanish,
French,
German
enum Languages 
English = 3,
Spanish = 10,
French = 23,
German = 33
enum TimeZones 
India = "IST",
UK = "GMT",
Ukraine = "EET"
console.log(TimeZones.India) // IST
const getTimezoneForIndia = perform() 
return 'IST';
enum TimeZones
India = getTimezoneForIndia(),
UK,
Ukraine,
// Error - UK, Ukraine will return undefined
const getTimezoneForUkraine = perform() 
return 'EET';
enum TimeZones
India,
UK,
Ukraine = getTimezoneForUkraine(),
enum Languages 
English = 1 + 1, // Returns "2"
Spanish = 1 * 5, // Returns "5"
French // Returns "6"
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"

More Posts