microsoft / microsoft/TypeScript

3.3.1 regression in type inference

Aperta
#29,743 8 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Type Inference
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

TypeScript Version: 3.3.1 & 3.4.0-dev.20190202

Search Terms:

Code

class Foo {
    foo: number = 0;
}

class FooBar extends Foo {
    bar: number = 10;
}

async function foo(f: Foo | number) {
    if (f instanceof FooBar) {
        try {
            f = await (async (): Promise<Foo | number> => {
                throw new Error();
            })();
        }
        catch (ex) {
            console.log(f.bar); // 3.3.1 has an error on this line -- "Property 'bar' does not exist on type 'number | Foo'. Property 'bar' does not exist on type 'number'"
        }
    }
}

foo(new FooBar());

Expected behavior:

No error as was in TypeScript 3.2.4

Actual behavior:

Error Property 'bar' does not exist on type 'number | Foo'. Property 'bar' does not exist on type 'number'

Playground Link:
http://www.typescriptlang.org/play/index.html#src=class%20Foo%20%7B%0D%0A%20%20%20%20foo%3A%20number%20%3D%200%3B%0D%0A%7D%0D%0A%0D%0Aclass%20FooBar%20extends%20Foo%20%7B%0D%0A%20%20%20%20bar%3A%20number%20%3D%2010%3B%0D%0A%7D%0D%0A%0D%0Aasync%20function%20foo(f%3A%20Foo%20%7C%20number)%20%7B%0D%0A%20%20%20%20if%20(f%20instanceof%20FooBar)%20%7B%0D%0A%20%20%20%20%20%20%20%20try%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20f%20%3D%20await%20(async%20()%3A%20Promise%3CFoo%20%7C%20number%3E%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20throw%20new%20Error()%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)()%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20catch%20(ex)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20console.log(f.bar)%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Afoo(new%20FooBar())%3B

Related Issues:

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci l'esempio dal TypeScript Playground collegato usando TypeScript 3.3.1, quindi confronta il suo comportamento con 3.2.4 e 3.4.0-dev.20190202. Traccia il modo in cui l'inferenza dei tipi gestisce il narrowing di instanceof attraverso l'assegnazione await e il blocco catch; il lavoro è completato quando f.bar non segnala più un errore nell'esempio riportato.

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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.