Because you expect EventType.A || EventType.B
evaluate to true, you should explicitly use true
in your switch
statement.
enum EventType {
A = "A",
B = "B",
C = "C",
}
function Test1(type: EventType) {
switch(true) {
// you should use next approach if you want to mix boolean end enums
case type === EventType.A || type === EventType.B:
console.log("Test1", type);
break;
case type === EventType.C:
console.log("Test1", type);
break;
}
}
function Test2(type: EventType) {
switch(type) {
case EventType.A:
case EventType.B:
console.log("Test2", type);
break;
case EventType.C:
console.log("Test2", type);
break;
}
}
const e = EventType.B;
Test1(e); // Expect "B" to print and it does it
Test2(e); // Expect "B" to print and it does
TypeScript like JS does not have advanced swith patterns like Rust, F#, Reason etc ...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…