对象值中的流联合未按预期工作

Flow union in object values not working as expected

我有以下代码。出于某种原因,Flow 拒绝了它。

class A {}
class B {}

type Intersection = (A | B);

var myMap: {
    a: A;
    b: B;
} = {
    a: new A(),
    b: new B()
}

var getter = function (name: string): () => Intersection {
    return function (): Intersection {
        return myMap[name];
    }
}

var bGetter: () => B = getter("b");

我发现代码没有错误。但是,Flow 拒绝了它:

/srv/webwallet/app/scripts/angularHelper.js:14:22,22: A This type is incompatible with /srv/webwallet/app/scripts/angularHelper.js:12:7,7: B

Found 1 error

为什么代码不校验,如何校验?

你需要改变

type Intersection = (A | B);

type Intersection = (A & B);

“|”运算符是一个不相交的联合。