facebook / facebook/flow

Union type inferring is somewhat incomplete

Open
#6,552 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

I've reproduced the bug using the online Flow type checker. Please refer to the following code:

```js
type TestType = {
type: "a",
a: number
} | {
type: "b",
b: number
};

const test = (a: TestType, b: TestType) => {
if (a.type === "a" && a.type === b.type) {
return b.a;
}
return 0;
}
```

This gives me an error:

```
11: return b.a;
^ Cannot get `b.a` because property `a` is missing in object type [1].
References:
9: const test = (a: TestType, b: TestType) => {
^ [1]
```

However, this gives no errors:

```js
type TestType = {
type: "a",
a: number
} | {
type: "b",
b: number
};

const test = (a: TestType, b: TestType) => {
if (a.type === "a" && b.type === "a" && a.type === b.type) {
return b.a;
}
return 0;
}
```

It should be inferred that because `a.type === "a" && a.type === b.type`, therefore `b.type === "a"`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.