简体   繁体   English

为什么枚举上的 switch 语句会抛出“无法与类型相比”的错误?

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

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.在做关于 Typescript 的 Lynda 教程( https://www.lynda.com/Visual-Studio-tutorials/TypeScript-types-part-2/543000/565613-4.html#tab )时,我遇到了障碍。 The sample code illustrates how switch statements work in TypeScript but the code that seems to work fine for the instructor throws a Type 'x' is not comparable to type 'y' error.示例代码说明了 switch 语句在 TypeScript 中的工作方式,但似乎对教师来说工作正常的代码抛出了类型“x”与类型“y”错误不可比 Here's the code:这是代码:

enum temperature{
    cold,
    hot
}

let temp = temperature.cold;

switch (temp) {
    case temperature.cold:
        console.log("Brrr....");
        break;
    case temperature.hot:
        console.log("Yikes...")
        break;
}

I get an error and squiggles under case temperature.hot: saying:我收到一个错误并在case temperature.hot:闪烁case temperature.hot:说:

Type 'temperature.hot' is not comparable to type 'temperature.cold'

What gives?什么给?

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).那是因为编译器已经知道 case temperature.hot永远不会发生:变量temp被赋予枚举文字类型temperature.cold ,它只能被分配给该值本身(如果没有严格的 null 检查,则为 null)。 As temperature.hot is not a compatible value here, the compiler throws an error.由于这里的temperature.hot不是一个兼容的值,编译器会抛出一个错误。

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;
}

Another possible reason could be if you check if the enum variable is not null but this also triggers if enumVal is 0 .另一个可能的原因可能是,如果您检查 enum 变量是否不为null但如果enumVal0也会触发。 This only holds true if the enum has the default numeric values (so first item gets the value of 0 )这仅在枚举具有默认数值时才成立(因此第一项的值为0

if (!!enumVal) {
    switch (enumVal) {
         case EnumClass.First // the if clause automatically excludes the first item

In my case the problem was as @Kris mentioned.在我的情况下,问题是@Kris 提到的。
I had to add我不得不添加

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

or change the first value of the enum to be 1 instead of 0或将枚举的第一个值更改为 1 而不是 0

export enum ICON_TYPE {
    ICON_A = 1,
    ICON_B = 2
}

This answer is very good however it does not give an example of casting.这个答案非常好,但是它没有给出铸造的例子。 Here I use casting to disregard the compiler's knowledge of the constant debug_level .在这里,我使用强制转换来忽略编译器对常量debug_level的了解。

type DebugLevel = 'log' | 'warn' | 'error';

const debug_lvl: DebugLevel = 'warn';

switch((debug_lvl as DebugLevel)){
  case 'error':
    console.error(...args);
    break;

  case 'warn':
    console.warn(...args);
    break;

  case 'log':
    console.log(...args);
    break;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript中switch语句上的`type is not compatible to type`错误 - `type is not comparable to type` error on switch statement in TypeScript Switch 语句 - 类型“数字”与类型“字符串”不具有可比性 - Switch Statement - Type 'number' is not comparable to type 'string' 为什么 TypeScript 在检查联合类型的 if 语句中会抛出错误,就好像它只是文字类型一样? - Why does TypeScript throw an error in an if statement checking an Union Type as if it's just a Literal Type? 为什么这个 map 声明会引发类型错误? - Why does this map declaration throw a type error? Switch 语句中 Enum 的 TypeScript 编译器错误 - TypeScript compiler error for Enum in Switch statement 为什么 Typescript 无法根据枚举 switch 语句推断出正确的类型? - Why is Typescript unable to infer the correct type based off an enum switch statement? 为什么 TypeScript 不会为未定义的类型抛出编译错误 - Why does TypeScript not throw a compile error for an undefined type 为什么 Typescript 会抛出错误信息:类型上不存在属性? - Why Typescript throw error message: Property does not exist on type? 当 function 参数类型不匹配时,为什么 TypeScript 不抛出错误? - Why does TypeScript not throw an error when the function argument type is mismatched? 为枚举类型使用常量时,为什么打字稿会出错? - Why does typescript give an error when using a constant for an enum type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM