57
loading...
This website collects cookies to deliver better user experience
enum Fruit {
APPLE = '🍎',
BANANA = '🍌',
CHERRY = '🍒',
}
|
operator:enum OtherFruit {
DATE = 'date',
}
type AnyFruit = Fruit | OtherFruit
type YummyFruits = Exclude<Fruit, Fruit.CHERRY>
// => type YummyFruits = Fruit.APPLE | Fruit.BANANA
type FruitKey = keyof typeof Fruit
// => type FruitKey = "APPLE" | "BANANA" | "CHERRY"
const keys = Object.keys(Fruit) as FruitKey[]
// => ["APPLE", "BANANA", "CHERRY"]
type FruitValue = `${Fruit}`
// => type FruitValue = "🍎" | "🍌" | "🍒"
const values: FruitValue[] = Object.values(Fruit)
// => ["🍎", "🍌", "🍒"]
for (let fruit of Object.keys(Fruit)) {
console.log(fruit)
}
// => APPLE
// BANANA
// CHERRY
for (let fruit of Object.values(Fruit)) {
console.log(fruit)
}
// => 🍎
// 🍌
// 🍒
var Fruit;
(function (Fruit) {
Fruit["APPLE"] = "🍎";
Fruit["BANANA"] = "🍌";
Fruit["CHERRY"] = "🍒";
})(Fruit || (Fruit = {}));
const enum
.const
in front of our Fruit enum makes a big difference:const enum Fruit {
APPLE = '🍎',
BANANA = '🍌',
CHERRY = '🍒',
}
const chosenFruit = Fruit.BANANA
// => compiles to:
// var chosenFruit = "🍌";