facebook / facebook/flow

Union type restrictions don't work when used within other objects

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

Description

Easiest to explain with code:

```jsx
type MyType = "A" | "B"
type WrappedType = {
myType : MyType
}

const x : MyType = "A";
const y : WrappedType = {
myType : x
}

if (x === 'C') { //Error: string literal `C`. This type is incompatible with string enum
//...
}

if (y.myType === 'C') { //No error, even though this is just as invalid as the above
// And from here `y` is treated as an `Object` type, meaning that any property access is valid:
const z = y.x * 3; // No error, even though this is obviously invalid
}
```

It seems to me that the behavior of the second conditional is a bug. Or is there any reason why flow doesn't detect the errors here?

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.