microsoft / microsoft/TypeScript

'instanceof' changes type outside of 'if' statement

Aperta
#31,155 6 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@orta ci sta già lavorando.

Dal 12/6/2019.

Bug Domain: check: Control Flow Rescheduled
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

TypeScript Version: 3.4.*

Search Terms:

instanceof

Code

// @strictNullChecks: true
interface OnChanges {
    onChanges(changes: Record<string, unknown>): void
}
interface Validator {
    validate(): null | Record<string, unknown>;
}

class C {
    validate() {
        return {}
    }
}
function foo() {
    let v: Validator & Partial<OnChanges> = null as any;
    if (v instanceof C) {
    }
    if (v.onChanges) { // error here
        v.onChanges({});
    }
}

Expected behavior:

No error.

Actual behavior:

Property 'onChanges' does not exist on type 'C | (Validator & Partial<OnChanges>)'.
  Property 'onChanges' does not exist on type 'C'.

instanceof shouldn't change the type of the variable outside of the if statement.

This only happens with strictNullChecks enabled.

Playground Link: https://typescript-play.js.org/#code/JYOwLgpgTgZghgYwgAgPIgMIAs4gOYQDOyA3gFDKXID2mO+RAFAvQYQFzIBKEC1UAEwA8hMFFB4ANMgCuIANYhqAdxAA+AJScAbtWACyAXzKhIsRCgBqcADb64YfqQpVtt+5EZbkIGTZvIAD7cvPzCouL40nKKKuoA3EZkZAg2cITEGM5UyG52Ag4QXtk5VFAQYDJQIKTGOcbGMHIIYMC0yDDU1MXkOTYVuZzW+Q5OAGTIAApwUK22QujYuGxqyAC8Pn4B6ci4AJ6JOcAwyIzayKCiuEjUJxgaJZR1VMen2gB0tEsMhA+9pZQPl9WEwSIYNIcqA0gA

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.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.