DEV Community

Discussion on: Why does typescript not conditionally pick the right type in a union?

Collapse
 
macsikora profile image
Pragmatic Maciej

So the proper typying of this case is:

interface PayloadA {
  type: 'ADD';
  propA: string;
}

interface PayloadB {
  type: 'UPDATE';
  propB: number;
}

type Action = PayloadA | PayloadB 

We need to join type with prop, in other way TS is not able to understand what property is in add action or update action. Such construct is known as sum type or discriminated union.

Thread Thread
 
thibmaek profile image
Thibault Maekelbergh

Thanks, I'll try this