I gave an example
class Foo {} class Bar {} declare var f: ((x: Foo) => void) & ((x: Bar) => void); f(new Foo());
on the documentation page https://flowtype.org/docs/union-intersection-types.html#_
And this type of code checks without errors.
For me, the result is not immediately obvious.
As they show somewhere near the top of the page with another example:
type I = {a: number} & {b: number}; var x: I = {a: 1, b: 2}; x = {a: 1, b: 2, c: "three"};
the intersection (which follows from the semantics of the semester itself) is a combination of 2 (or more) types. Mostly AND of them.
So why f(new Foo()); doesn't cancel type checking? The new Foo() argument is clearly not an instance of Bar , so it should not pass.
What am I missing?
UPD
After several more studies, I found that the value of | and & swaps when you use declare var (compared to type or in place). I canβt find an explanation why this happens even in the very first place.
source share