类型 'X' 是不 comparable 到类型 'Y'
错误信息
类型是 不 comparable 到类型
快速摘要
你 compared two 值谁的类型有 否 overlap — comparison 将总是是 假 or 真. TypeScript flags 这个 as likely logic 错误.
为什么发生
Comparing 字符串到 数字使用 ===
Comparing two 不同 literal 类型
Comparing 值到 类型它 能从不是
Logic 错误 in 类型 narrowing
最小示例
✗ 错误代码
const x: string = "hello";
if (x === 42) { // always false: string vs number
console.log("never reached");
}✓ 修复代码
const x: string | number = "hello";
if (x === 42) {
console.log("x is 42");
}Widen 类型到 包含两者都字符串 and 数字, or 移除不可能 comparison.
如何诊断
检查类型的 两者都边 的 comparison
Determine 如果 comparison 是 logically meaningful
Look for 类型 narrowing 那个可能有 制作 comparison 不必要
如何修复
更改 comparison 到 use compatible 类型
移除 comparison 如果它's 总是真/假
Use 类型 assertion 如果类型是 actually compatible at 运行时
Fix 类型 annotations 那个 led 到不可能 comparison
如何预防
Use strict equality (===) 到 catch 类型 mismatches
启用 strict 模式 for better 类型检查