facebook / facebook/flow

Flow can't distinguish shapes of union types when testing the shapes of properties within the subtypes

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

Description

You may see this bug in action at https://flow.org/try/#0C4TwDgpgBAYg9nKBeKBvAUFKwAWBLAOwHMAuKAcl0KPIBp0BfAbnXVElgQBtk1MoARtTLkhxOoxZtw0AEIBDAE68MWCAQCuAWxHrtErADd5XDRDLw49Zq3Zz5ALxX89Oiq7pQA9F6gBNCABnWigASSgAawI4AHcoPGByQOwcaED5LQgQgQ1geLzAnDgNLgATKEDgPC4eAWh5AS5oYERgCBqofixunt6fKAAzRTgtFOgwYchFUCgAY1TZiP5jU3NOOC5rKX7QgnitMCbMgmB5KrgCVgGNAlnzvYGCAAowJQyyBWUAHygFBwBKMgAfksfCweAGUBeby0ADoVmZYVRiMgkChKPhxP8wd1FBBgBpFHtXooMvCTGYWFgGPx+HiCUSoJoaiwGEA

Code is here:
```
type Foo = {
thing: 'thing',
};

type Fool = {
bing: 'bing',
};

type Bar = {
enum: 'enum',
value: Foo,
};

type Baz = {
enum: 'enum', // Yes, I know it's the same, but it should still be able to tell
// from the property check
value: Fool,
};

// In implementation

function fn(param: Bar | Baz): ?Foo {
if (param.value.thing === 'thing') {
return param.value;
}

return null;
}
```
Error:
```
22: function fn(param: Bar | Baz): ?Foo {
^ property `thing`. Property not found in
24: return param.value;
^ object type
```

Hard to explain, but in essence it seems as though when Flow needs to check a property of a subtype of a union in order to determine the subtype, it fails.

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.