facebook / facebook/flow

Warn about dead code

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

Description

```
type someType = {
a : number,
b : string | null
}

//...

const ff = (data: someType): string => {
if (data.c === null) {
return 'field b is smpty';
}

return 'some value';
}
```

At this moment for flowtype this code is correct. Only that this code contains a logical error.
Flowtype could easily detect this error.

```
data.c === null
```
Left side is undefined (data.c is always undefined), right side is null. This condition always returns false.
So enough detect that the types specified for the operator compare are disjoint.

Another example:

```
type someStatusType = 'loading' | 'error' | 'ok';

const getLabel = (status: someStatusType): string => {
if (status === 'loading') {
return 'Loading';
}
if (status === 'error') {
return 'Error label';
}
if (status === 'complete') { //dead code
return 'Complete status';
}
return 'ok';
};
```

This dead code can be detected by this comparison :
```
status === 'complete'
```
This types are disjoint ( 'loading' | 'error' | ok' === 'complete' will always be false).

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.