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

159
Views
Flow JS Union types and callbacks

I have a flow curiosity regarding union types and callbacks, where it seems logically to me that the error cannot happen given the circumstances. I'm wondering if I'm having a brainfart, if flow just doesn't pick up such things, or it's something else

I've boiled the usecase down to it's core with the following Try Flow Link, but I'll add it below for posterity. The case is when I have a function that takes in a union type as well as a callback that takes in the same union type.

If I have a separate function that restricts the type to just one of the types in the union, flow still complains that the callback could have either. It seems to me that, technically, Flow should have enough information statically to understand this can't happen, but again, maybe I'm brainfarting.

type A = {
  id: number,
  aOnly: boolean,
} 

type B = {
  id: number,
  bOnly: boolean
}

type AorB = A | B;

/**
  Main fn that takes in union type, as well as a callback that accepts
  the union type
 */
function main(ab: AorB, callback: (AorB) => boolean): void {
  callback(ab);
}


/**
  ONLY allows type A, therefore the callback should only be receiving type A
  in this scenario, yet flow thinks it could have either type
 */
function processA(a: A): void {
  main(a, (a) => {
    /* Cannot get `a.aOnly` because property `aOnly` is missing in `B` */
    return a.aOnly;
  });
}

/**
  Running the following gets:
  Cannot get `a.aOnly` because property `aOnly` is missing in `B`
 */
processA({
  id: 123,
  aOnly: true
});

What exactly is happening here? Is there a solution to this that doesn't involve adding in conditionals or disjoint unions?

about 4 years ago · Juan Pablo Isaza
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!