TypeScript Version:
nightly (1.9.0-dev.20160217)
Code
interfaceThing{thing: string;}functionisThing(x: any): x is Thing{returnx&&typeofx.thing==='string';}functionfoo(x: Thing|string){letbranch: number;letresult: string;if(typeofx==="string"){branch=1;result=x;// OKif(isThing(x)){branch=2;result=x.thing;// ERROR: 'thing' does not exist on type 'string'}}else{branch=3;result=x.thing;// OKif(typeofx==="string"){branch=4;result=x// ERROR: 'Thing|string' is not assignable to type 'string'}}return`(${branch}) ${result}`;}leta=foo('string a');letb=foo({thing: 'thing b'});String.prototype['thing']='thing c';letc=foo('string c');console.log(a,b,c);// outputs "(1) string a (3) thing b (2) thing c"Expected behavior:
No compile-time or runtime errors, and outputs "(1) string a (3) thing b (2) thing c"
Actual behavior:
No runtime errors, and outputs "(1) string a (3) thing b (2) thing c" as expected, but compiler issues two errors as shown in code comments.
TypeScript Version:
nightly (1.9.0-dev.20160217)
Code
Expected behavior:
No compile-time or runtime errors, and outputs
"(1) string a (3) thing b (2) thing c"Actual behavior:
No runtime errors, and outputs
"(1) string a (3) thing b (2) thing c"as expected, but compiler issues two errors as shown in code comments.