facebook / facebook/flow

Combination of switch case and refinement of possibly undefined variable doesn't work

Open
#5,145 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

```js
//@flow

function getMaybe (): string|void {
return;
}

function doSomething (): string {
const target = getMaybe();
switch (true) {
case (target !== undefined):
return target;
default:
throw new Error('The target was undefined!');
}
}
```

The target within the case body should be refined that it cannot be undefined anymore. This also does not work when using `target instanceof SomethingElse` or `typeof target === 'notundefined'`.

https://flow.org/try/#0PTACDMBsHsHcCh7gK4DsDGAXAltVACAcwFNMBZAQwE8AjY-ACgEoAufAZ0wCdtVCAfAG7RsAE3wBvePnxdSyLqgDc8AL6IUGHHnyjoAZWgBbUgAtehRqw7cLk6fnR5O+TBS4lM+ALxFSlWmJmFRl2WGxMdFNGbmRiJnsZdAp2egY3D1J8AEJvXzRRYnBeYlFWBxk5TAUCDM8Q3SKKZEhMFgrXUy44fFRiWHwAUS5urgYAcgAVU3o6rNgU-AKiktFs8aYG9XUgA

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.