

7 TypeScript Tricks with Enum (and Fruits) 🧺
source link: https://dev.to/prod/mastering-enums-in-typescript-1c1j
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Enums are one of the key features of TypeScript.
Relying on basic enums, (numeric and auto-incremented) is standard and the most common use case. Yet Enums can offer much more.
In the rest of this post, let's assume the following code is available:
enum Fruit {
APPLE = '🍎',
BANANA = '🍌',
CHERRY = '🍒',
}
1. Merge enums
Merging enums is pretty straightforward using the pipe |
operator:
enum OtherFruit {
DATE = 'date',
}
type AnyFruit = Fruit | OtherFruit
2. Enum subsets
Because cherries may not be as tasty as other fruits, let's exclude them:
type YummyFruits = Exclude<Fruit, Fruit.CHERRY>
// => type YummyFruits = Fruit.APPLE | Fruit.BANANA
3. Get the keys of an enum dynamically
This one needs the use of two type operators: keyof
and typeof
.
type FruitKey = keyof typeof Fruit
// => type FruitKey = "APPLE" | "BANANA" | "CHERRY"
const keys = Object.keys(Fruit) as FruitKey[]
// => ["APPLE", "BANANA", "CHERRY"]
4. Get the values of an enum dynamically
This snippet leverages the Template Literal type operator:
type FruitValue = `${Fruit}`
// => type FruitValue = "🍎" | "🍌" | "🍒"
const values: FruitValue[] = Object.values(Fruit)
// => ["🍎", "🍌", "🍒"]
5. Iterate over an enum keys
Looping through the enum keys is as simple as:
for (let fruit of Object.keys(Fruit)) {
console.log(fruit)
}
// => APPLE
// BANANA
// CHERRY
6. Iterate over an enum values
In the same spirit, looping through the enum values:
for (let fruit of Object.values(Fruit)) {
console.log(fruit)
}
// => 🍎
// 🍌
// 🍒
7. const enum
By default, enums generate a bunch of code when compiled to JavaScript:
var Fruit;
(function (Fruit) {
Fruit["APPLE"] = "🍎";
Fruit["BANANA"] = "🍌";
Fruit["CHERRY"] = "🍒";
})(Fruit || (Fruit = {}));
There is however a way not to generate this much code: by leveraging const enum
.
Adding just a const
in front of our Fruit enum makes a big difference:
const enum Fruit {
APPLE = '🍎',
BANANA = '🍌',
CHERRY = '🍒',
}
...as it compiles to nothing. 🕳️
Just until we use part of its values:
const chosenFruit = Fruit.BANANA
// => compiles to:
// var chosenFruit = "🍌";
Recommend
-
4
Are any vegetables also fruits? Potatoes and other root vegetables are plant roots, but they are not seeds. But, peas, beans, and other ‘legumes’ are actually considered fruit and the seeds are the peas and beans inside the pod.
-
5
Are fruits seeds? No, they develop from the flower’s ovary – and the hard pips inside are the seeds. Juicy fruits such as berries are called ‘true fruits’ because they are made from the flower’s ovaries alone. Apples and pears are...
-
9
聊聊TypeScript中枚举对象(Enum)什么是枚举,顾名思义我们看到枚举这个词,脑子里就已经想到这是一个键值对的形式,就可以看做是我们JavaScript中的json对象一...
-
12
How to watch Fruits Basket online from anywhere Fruits Basket is certainly one of the more unique anime concepts out there, offering a plot that is heartfelt, sad and at times... slightly confusing. A must-watch for both new and exist...
-
14
Sell your digital contents in 2 minutes!Fruits allows you to sell your digital products on your website, your social media channels, in your...
-
7
The Strange Fruits of CapitalismGiving birth to the bizarre
-
7
Should We Gene-Edit Our Fruits and Vegetables?July 30th 2022
-
5
Fruits and vegetables company Dole suffers ransomware attack...
-
7
Pursuing low-hanging fruits might be a bad ideaHow to balance low-hanging fruits and big bets for the best outcomes.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK