Why does a switch statement on an enum throw 'not comparable to type' error?

后端 未结 3 378
梦毁少年i
梦毁少年i 2021-01-04 03:29

While doing a Lynda tutorial on Typescript ( https://www.lynda.com/Visual-Studio-tutorials/TypeScript-types-part-2/543000/565613-4.html#tab ), I hit a snag. The sample code

相关标签:
3条回答
  • 2021-01-04 03:56

    Another possible reason could be if you check if the enum variable is not null but this also triggers if enumVal is 0. This only holds true if the enum has the default numeric values (so first item gets the value of 0)

    if (!!enumVal) {
        switch (enumVal) {
             case EnumClass.First // the if clause automatically excludes the first item
    
    0 讨论(0)
  • 2021-01-04 04:06

    That's because the compiler already knows that the case temperature.hot will never happen: the variable temp is given the enum literal type temperature.cold, which can only be assigned that value itself (or null if there are no strict null checks). As temperature.hot is not a compatible value here, the compiler throws an error.

    If we discard the information about the literal (by casting or retrieving the value from a function):

    function how_cold(celsius: number): temperature {
        return celsius > 40 ? temperature.hot : temperature.cold;
    }
    

    The code will then compile:

    let temp = how_cold(35); // type is "temperature"
    
    switch (temp) {
        case temperature.cold:
            console.log("Brrr....");
            break;
        case temperature.hot:
            console.log("Yikes...")
            break;
    }
    

    Alternatively, prepending + to the value works because it converts the value to a number, which will also widen the type's scope and make it compatible with all enum variants, as well as other numbers.

    let temp = temperature.cold;
    
    switch (+temp) {
        case temperature.cold:
            console.log("Brrr....");
            break;
        case temperature.hot:
            console.log("Yikes...")
            break;
        case 5:
            console.log("What??");
            break;
    }
    
    0 讨论(0)
  • 2021-01-04 04:12

    In my case the problem was as @Kris mentioned.
    I had to add

    if (icon !== null) {
        switch (icon) {
    

    or change the first value of the enum to be 1 instead of 0

    export enum ICON_TYPE {
        ICON_A = 1,
        ICON_B = 2
    }
    
    0 讨论(0)
提交回复
热议问题