microsoft / microsoft/TypeScript
Type narrowing on object properties lost in async IIFE
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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).
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci l’esempio dell’issue nel TypeScript Playground collegato usando la versione segnalata 3.4.0-dev, confrontando i casi di narrowing di x e y.z intorno all’async IIFE. Traccia il narrowing del flusso di controllo del compilatore per le proprietà degli oggetti e aggiungi una verifica affinché la chiamata res4 accetti y.z come string senza modificare il comportamento esistente di res2 e res3.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100