facebook / facebook/flow

[bug] Intersection with a tagged union unable to refine type

Open
#3,391 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

```js
/* @flow */

type A = {a: string}
type B = {b: string, type: 'b'}
type C = {c: string, type: 'c'}
type BorC = B | C
type ABC = A & BorC

const m: ABC = {type: 'b', a: '', b: ''}

if (m.type === 'b') {
m.b // Error!
// Also interesting. If you have this line, then the `const m` line gets
// a flow error. Remove the line, and the error on `const m` goes away.
}
```

[run it on tryflow](https://flowtype.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgCCYAvGAN4CGAXGAM4YBOAlgHYDmAvprgQEKkKAI1oMWHADRhs+WgHIhc7jIIBhQeQDGopm3ZSV8zUp74wfOI3VkBAHzCrTBQn2tEwAMnOXHKogLJiLz4nIjcgh1CXS0JwuzD0TThWBjAAW1oXN3JDMAU5KRE8ygKwGjyTYFBUZigwAAo0gDo-Eja8xQBKClQwdKahVG4q5DRUJJSMMFZMi0ZYjVz8qXK5UqK17lqG1hbeUnb87vJe6YGhoA)

However, a union of intersections works fine
```js
type A = {a: string}
type B = {b: string, type: 'b'}
type C = {c: string, type: 'c'}

type AB = A & B
type AC = A & C
type ABorAC = AB | AC

const n: ABorAC = {type: 'b', a: '', b: ''}
if (n.type === 'b') {
n.b // no error
}
```

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.