Skip to content

TS4116: 类型是 不包含 in union

错误信息

                类型 'X' 是不 包含 in union 类型的 discriminant 属性.
              

快速摘要

你的切换情况值 doesn't match 任何 union 成员's discriminant 属性值.

为什么发生

  • 切换情况值 doesn't match 任何 union 成员's discriminant

  • Typo in 情况值

  • 新 union 成员添加但 切换不 更新

最小示例

✗ 错误代码
type Shape = { kind: 'circle' } | { kind: 'square' };
function area(s: Shape) {
  switch (s.kind) {
    case 'circle': return 0;
    case 'square': return 0;
    case 'triangle': return 0; // TS4116
  }
}
✓ 修复代码
type Shape = { kind: 'circle' } | { kind: 'square' };
function area(s: Shape) {
  switch (s.kind) {
    case 'circle': return 0;
    case 'square': return 0;
  }
}

移除情况那个 doesn't match 任何 union 成员.

如何诊断

  • 检查 discriminant 属性值 in union 类型

  • Compare 针对切换情况值

如何修复

  • Fix 情况值 到 match 有效 union 成员

  • 移除情况如果它's 不需要

  • 添加新 成员到 union 类型如果它's 新情况

← 返回语言