function discards outer type-check on union type · Issue #13560 · microsoft/TypeScript (original) (raw)
TypeScript Version: 2.2.0-dev.20170113
Code
class test1 { someProp; } class test2 { anotherProp; }
let myTest: test1 | test2; if (myTest instanceof test1) { // valid myTest.someProp;
// invalid, someProp does not exist on type 'test2'
() => myTest.someProp;
// meaning if you have a eg. forEach
[].forEach(() => {
// you will need an instance check here too
// invalid
myTest.someProp;
// valid
if (myTest instanceof test1)
myTest.someProp;
})
}
Expected behavior:
I expected typescript to know myTest was instanceof test1 inside the function. It did not give these errors in typescript 1.8
Actual behavior:
It lost this knowledge inside the function.