facebook / facebook/flow

Warn about dead code

Offen
#3,492 5 Kommentare 13 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Rust
Sterne
22.3k
Forks
1.9k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

```
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).

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.