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

后端 未结 3 487
梦毁少年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
    

提交回复
热议问题