facebook / facebook/flow

Dynamic `typeof` test with union types outside do not carry into closure scope

Offen
#3,369 2 Kommentare 0 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

Hi, I'm running into an issue that I think may be a bug. It seems like dynamic `typeof` tests on union types aren't respected within closures.

For example:

```
function b(url: string | Object) {
if (typeof url === 'string') {
return new Promise(function(resolve, reject) {
var request = new XMLHttpRequest();
request.open('GET', url)
});
}
}

// For some reason it works when request is shadowed
function b(url: string | Object) {
if (typeof url === 'string') {
// request is shadowed
return new Promise(function(resolve, request) {
var request = new XMLHttpRequest();
request.open('GET', url);
});
}
}

// Another closure via function declaration
function b(url: string | Object) {
if (typeof url === 'string') {
const a = function(resolve, reject) {
const request = new XMLHttpRequest();
request.open('GET', url)
};
}
}
```

gives the errors

```
5: request.open('GET', url)
^ object type. This type is incompatible with the expected param type of
[LIB] static/v0.39.0/flowlib/bom.js:488: open(method: string, url: string, async?: boolean, user?: string, password?: string): void;
^ string
26: request.open('GET', url)
^ object type. This type is incompatible with the expected param type of
[LIB] static/v0.39.0/flowlib/bom.js:488: open(method: string, url: string, async?: boolean, user?: string, password?: string): void;

```

I would expect that due to the `typeof` test on `url` outside the scope, and because i'm not reassigning `url`, this would return no errors.

Thanks for all of your continued great work! Please let me know if I'm making a mistake here.

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

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