【发布时间】:2018-03-22 17:19:17
【问题描述】:
让开关为枚举的判别联合工作的最简单方法是什么? 基本上我正在尝试模拟某种模式匹配。
enum Fruit {
Apple,
Banana,
Orange
}
enum Vegetable {
Tomato,
Carrot,
Potato
}
type Grocery = Fruit | Vegetable;
function checkStuff(grocery: Grocery) {
switch (grocery.kind) {
case "Fruit":
doStuff(grocery);
break;
case "Vegetable":
doOtherStuff(grocery);
break;
default:
break;
}
}
【问题讨论】:
标签: typescript enums pattern-matching discriminated-union