Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

78
Views
Narrowing typescript generic type based on discriminated unions

I have some type definitions as shown below.

type PayloadType = 'A' | 'B';

interface Payload<T extends PayloadType> {
  type: T;
}

interface PayloadA extends Payload<'A'> {
  state: string
}

interface PayloadB extends Payload<'B'> {
  serialNumber: string;
}

type TPayload = PayloadA | PayloadB;


type PayloadInterpretation<T extends TPayload> = {
  payload: T;
  entries: T[]; // This property is only for demonstration purpose
};

type TPayloadInterpretation = PayloadInterpretation<PayloadA> | PayloadInterpretation<PayloadB>;

function f(interpretation: TPayloadInterpretation) {
  if (interpretation.payload.type === 'B') {
    const payload = interpretation.payload; // payload is of type PayloadB
    const entries = interpretation.entries; // entries is of type PayloadA[] | PayloadB[]
  }
}

The comments show that even the type of payload can be correctly narrowed down to PayloadB based on discriminated unions, but the type T[] for entries is still PayloadA[] | PayloadB[].

I was thinking if typescript knows the type T for payload is PayloadA, it should also be able to narrow entries: T[] to entries: PayloadB[]. I know I could do a type casting like:

function f(interpretation: TPayloadInterpretation) {
  if (interpretation.payload.type === 'B') {
    const payloadBInterpretation = interpretation as PayloadInterpretation<PayloadB>;
    ...
  }
}

But my question would be is there any other way to do this?

The code is here in typescript playground.

Thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When you check for interpretation.payload.type, you are only narrowing the interpretation.payload object. You are not actually doing anything to narrow interpretation.entries.

In other words, typescript doesn't know that interpretation.entries can also be narrowed when you narrow interpretation.payload.

If you want both of them to be narrowed, you need another discriminator in the PayloadInterpretation type:

// ...

type PayloadInterpretation<T extends TPayload> = {
  type: T['type']; // the new discriminator for the whole PayloadInterpretation
  payload: T;
  entries: T[];
};

// ...

function f(interpretation: TPayloadInterpretation) {
  if (interpretation.type === 'B') { // narrowing the whole interpretation instead of only interpretation.payload
    const payload = interpretation.payload; // payload is of type PayloadB
    const entries = interpretation.entries; // entries is of type PayloadB[]
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!