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