2

我有以下代码。出于某种原因,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 此类型与 /srv/webwallet/app/scripts/angularHelper.js:12:7,7:B 不兼容

发现 1 个错误

为什么代码不检查,如何检查?

4

1 回答 1

3

你需要改变

type Intersection = (A | B);

type Intersection = (A & B);

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

于 2015-06-21T16:12:32.567 回答