Narrowed union types loosened within closures · Issue #19683 · microsoft/TypeScript (original) (raw)

This could be a dup of #14748; they seem related

TypeScript Version: 2.7.0-dev.20171102

Code

See in playground

class A { name: string; } class B { age: number; }

let x: A | B;

if (x instanceof A) { console.log(x.name); // ok: x has type A

setTimeout(() => {
    console.log(x.name); // ** COMPILE ERROR: x has type A | B here
});

} else { console.log(x.age); // ok: x has type B here

setTimeout(() => {
    console.log(x.age); // ** COMPILE ERROR: x has type A | B here
});

}

Expected behavior:

This should compile.

Actual behavior:

It does not compile