loading...

re: Why does typescript not conditionally pick the right type in a union? VIEW POST

TOP OF THREAD FULL DISCUSSION
re: OK, I realized it shortly after posting. However, how would you ever handle a typical case like a state reducer (in Redux for example) where your p...
 

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.

Code of Conduct Report abuse