Skip to content

Instantly share code, notes, and snippets.

@peat-psuwit
Created January 19, 2018 15:16
Show Gist options
  • Save peat-psuwit/7f399e9e9900bdd440570b9eca9af816 to your computer and use it in GitHub Desktop.
Save peat-psuwit/7f399e9e9900bdd440570b9eca9af816 to your computer and use it in GitHub Desktop.
Flow type refinement
/* @flow */
type A = {
x: number
};
type B = A & {
y: string
};
type C = A & {
z: string
};
function f(o: $Exact<A>|$Exact<B>): ?B {
if (o.y && o.x > 5)
return o;
else
return null;
}
function g(o: $Exact<B>): A {
return o;
}
function h(o: ?($Exact<B>|$Exact<C>)) {
(o: ?(B|C));
if (o)
(o: $Exact<B>|$Exact<C>);
if (o)
// A. This type is incompatible with union: `B` | `C`
(o: B|C);
if (o)
((o: $Exact<B>|$Exact<C>): B|C);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment