facebook / facebook/flow

Object destructuring breaks disjoint union type.

Open
#3,932 4 comments 5 reactions 0 assignees View on GitHub
destructuring Typing: unions/intersections
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

As an example:

```js
type Props =
| { type: 'foo', item: { id: number, parentId: number | null } }
| { type: 'bar', item: { id: number, parentId: number } }

function test(props: Props) {
// This works
const yay = props.type === 'foo'
? props.item.id
: `/parents/${props.item.parentId}/items/${props.item.id}`

// This does not
const { type, item } = props
const boo = type === 'foo'
? item.id
: `/parents/${item.parentId}/items/${item.id}`
}
```

I'm running v0.46. Curiously, the try flow REPL [doesn't exhibit an error](https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAodAXAngBwKZgAKATnDgM5gC8qYYAPmAN5jb4BcYA5FHHFwBowASwx4AtpxbCAJpwB2AV3EAjPMSE4AhsTzyMASTlglq9QxOKYMMAF87tCyzZ5OXFTsEixk5iOOmahpg2rr6RgrKQXYOdKhQivIAxhjCcPKseOQYABQ4pBScJGTkAJTMjnTAwGAAKgAWwpQIcMQA1uSVYEnp2WBYWljUIQXkAHQu1FRU3Lz8YF10dAD8IyVjohIbMmCci0sABsChehjkwAAkTPnrm+JjJ+EytsB351c3FBs+27YHXV1qnVGpQZHAsvIuBguj15H1nLg8EI7jEZhgQQ9RjDehgwIpiDY0YipjMeHwuAslmAAJCrO7bSlLamcI6PM6XJj0tlGF5vDn02R-VC2IA), I'm only getting it locally.

Any ideas?

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.