Cannot assign correctly typed member to map

Viewed 48

Although I generally understand the reason for Typescript errors and can fix them, this one has me flummoxed.

In this playground I rely on the answer from Can I define a Typescript map having a value constraint corresponding with each value's key? to create a map with correctly typed properties but I found that I cannot assign a member.

I would expect Typescript to be able to reason that it was assignable. The error is quite weird. Given that the property is optional I'd expect it to accept undefined, but not complain when it's a concrete object which is guaranteed to match the constraint. Within the populateLastActions the assignment lastAction[actionType] = action causes an error.

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 Answers
Related