Switch case with or expression on enum does not evaluate as expected

Viewed 709

I am trying to switch on an enum so that the correct code is run. I have made a typescript playground that showcases a small example of what I am trying to accomplish.

In the provided example I am trying to understand why Test1 will not print "B". My expectation is that when the logical OR is used it will try to check if either case is true. Not just the first one. Might this be a misunderstanding of some fundamentals? Test2 works because I am explicitly stating the cases, but Test1 would be a more compact version.

enum EventType {
    A = "A",
    B = "B",
    C = "C",
}

function Test1(type: EventType) {
    switch(type) {
        case EventType.A || EventType.B:
            console.log("Test1", type);
            break;
        case 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 but does not
Test2(e); // Expect "B" to print and it does

Typescript Playground

2 Answers

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 ...

Your problem is in fundamentals. The term you are looking for is called fall-through.

To evaluate multiple cases in a switch-statement for fall-through, you write multiple cases grouped with the same break.

Fall-through Example

switch(foo) {
 case A:
 case B:
  /* statements for both A and B */
 break;
 case C:
 /* statements for C */
}
Related