Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda