facebook / facebook/flow

Union Type and destructuring

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

Description

Looks like flow is losing type info when extracting some property of a Union Type variable:

```javascript
/* @flow */

type Action =
| {type: 'A', payload: string}
| {type: 'B', payload: number};

// OK
const f1 = (action: Action) => {
switch (action.type) {
case 'A': return action.payload.length;
case 'B': return action.payload + 10;
}
}

// Not OK
const f2 = ({type, payload}: Action) => {
switch (type) {
case 'A': return payload.length;
case 'B': return payload + 10;
}
}
```
[flow.org/try](https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgCCAxhgJZwB2YAvGKmGAD5gDe2+AXGAOSE8AaMDgCGWeCIAm3AM4YATmUoBzAL6MGzNhzzceAIUHCxE6WEoBXALYAjPPNUBudMGBgA8gGlUxKnLBQAIy0YAAUIqQUlNwk5FQAlLQAfGyaMghkGMQAFmERcZQAdDqJrJqMxCIyBHw83PJ4GBby1PlRhaLicFKFMHgqGNnOjBVVNYb1jc2tkVQdJt2SYADUYIEADMNgqqg7Lm4AcnAYHt6+lP5QAEwhoey4eEKdpqoxs5SJNClljOmZOWESqkRmBKtVePxJk0WsYuj0+gMhuVQWNeBMwA1odRnosVmtNpodqogA)

Is this a bug or the expected behavior?

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.