[英]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
但如果enumVal
为0
也会触发。 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.