facebook / facebook/flow

Conditional refinement doesn't work as expected

Open
#8,074 0 comments 0 reactions 0 assignees View on GitHub
feature request Typing: refinements
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Flow version: all including v0.107.0 and master

## Expected behavior

`No errors!`

## Actual behavior

The following code throws an error but it should not:

```js
const a: ?string = 'f';
const b: ?string = 'g';

function test(x: string) {
console.warn(x);
}

if (a == null && b == null) {
throw new Error('at least one is required');
}

if (b == null) {
// Cannot call test with a bound to x because null or undefined [1] is incompatible with string [2].
// Should be no error? Flow thinks 'a' still can be a nullable string here.
test(a);
}
```

[flow.org/try](https://flow.org/try/#0MYewdgzgLgBAhgLhgfmgJwJZgOYwLwwDkAZoQNwBQoksARkqlJjvkduRRcQK5jBQZwMKAFNoACgAeSdFmwBKGAG8KMGNQggANiIB0AdzhowU+ZQC+nDMRji4+AmG5atMAGRuYtBzCcvFKmpQABZoIPq+IhEAomhhaOKEcLA6cNAw4CIwGBAwaCIAjtwY+QAmhGYUlhTWtt54js5aAaowAPRtMADCcGBgILDAcC7CYrD6GCHwXiC8pcIgMJJeIkPcEFl+riBoMHMixFgi8wDaAIwAutm5WKAAtgAOyRi0OjATU7IsJwBMF7qtDowADKwVmWnmtE2ixEcR2yBgADEtOFhMEsABrXJJQgwaAYEZDMAraZbOCvLJfXDBWF6VqiCRwSqWIA)

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.