Given an enum in typescript
enum CoffeeSizes {
Large = 'L',
Medium = 'M',
ExtraLarge = 'XL',
}
CoffeeSizes.Large === 'L' // true
How do I do the reverse lookup, where I can say
CoffeeSizes.L === 'Large'
How can I create a type with the inverse enum?
Reverse mappings are only possible for numeric non const enums.
As in the example below
enum CoffeeSizes {
Large,
Medium,
ExtraLarge
}
const nameOffLargeCoffeeSize = CoffeeSizes[CoffeeSizes.Large];
CoffeeSizes[nameOffLargeCoffeeSize] === CoffeeSizes.Large;
You can get more details in TypeScript docs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.