Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda