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

问题描述

虽然我大致了解 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)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)