microsoft / microsoft/TypeScript
Type narrowing on object properties lost in async IIFE
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
TypeScript Version: 3.4.0-dev.20190327
Search Terms:
narrowing iife control flow
Code
declare const x: string | undefined;
declare const y: { z: string | undefined };
function needsString(it: string) { return it; }
function a() {
if (!x) {
throw new Error("Missing x");
}
const res1 = (() => needsString(x))(); // all good, per #8849
const res2 = (async () => needsString(x))(); // ditto
if (!y.z) {
throw new Error("Missing z.")
}
const res3 = (() => needsString(y.z))(); // still good
const res4 = (async () => needsString(y.z))(); // now things blow up
}
Expected behavior:
The call to needsString in the expression for res4 succeeds, like the one in res2 and res3. Even though the function is async, the code in it -- at least that's before an await -- runs immediately, I believe, and so should be subject to the narrowing (esp. given that res2 works).
Actual behavior:
Error for needsString(y.z): Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
Related Issues:
https://github.com/Microsoft/TypeScript/pull/8849, which I think was supposed to fix IIFE issues like this, but appears to have missed a case (or I'm missing something).
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere das Beispiel aus dem Issue im verlinkten TypeScript Playground mit der gemeldeten Version 3.4.0-dev und vergleiche die Narrowing-Fälle x und y.z rund um die async IIFE. Verfolge das Control-Flow-Narrowing des Compilers für Objekteigenschaften und ergänze Tests, sodass der Aufruf res4 y.z als string akzeptiert, ohne das bestehende Verhalten von res2 und res3 zu ändern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100