无法将正确类型的成员分配给映射

时间:2020-12-19 11:22:16

标签: typescript undefined assign discriminated-union

虽然我大致了解 Typescript 错误的原因并且可以修复它们,但这个问题让我感到困惑。

this playground 中,我依靠来自 Can I define a Typescript map having a value constraint corresponding with each value's key? 的答案来创建具有正确类型属性的地图,但我发现我无法分配成员。

我希望 Typescript 能够推断出它是可分配的。这个错误很奇怪。鉴于该属性是可选的,我希望它接受未定义,但当它是一个保证匹配约束的具体对象时不会抱怨。在 populateLastActions 中,赋值 lastAction[actionType] = action 导致错误。

const lastAction: {
    create?: CreateAction | undefined;
    fulfil?: FulfilAction | undefined;
}
Type 'Action' is not assignable to type 'undefined'.
  Type 'CreateAction' is not assignable to type 'undefined'.(2322)

0 个答案:

没有答案
相关问题