Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

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

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

question from:https://stackoverflow.com/questions/65934259/switch-case-with-or-expression-on-enum-does-not-evaluate-as-expected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...