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?