Flow union in object values not working as expected

580 views Asked by At

I have a following code. For some reason, Flow rejects it.

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");

I see no error in the code. However, Flow rejects it with the following:

/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

Why doesn't the code check, and how to make it check?

1

There are 1 answers

1
etosch On BEST ANSWER

You need to change

type Intersection = (A | B);

to

type Intersection = (A & B);

The "|" operator is a disjoint union.