You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search Terms: Type Inference, Union Type, Assignment Operation
Code
// Tested by running `tsc` on the command-line// strictNullChecks: oninterfaceBarA{label: true;yoha?(): string;}interfaceBarB{label: false;yoha?(): void;}classFoo{bar!: BarA|BarB;pia(): void{if(this.bar.yoha){this.bar.yoha();// passed compiling}if(this.bar.yoha){if(this.bar.label){letlabel=this.bar.yoha();// passed}else{this.bar.yoha();// passed}this.bar.yoha();// passed}letbar=this.bar;if(bar.yoha){bar.yoha();// passed}if(bar.yoha){if(bar.label){letlabel=bar.yoha();// error while compiling: Object is possibly 'undefined'.}else{bar.yoha();// error: Cannot invoke an object which is possibly 'undefined'.}bar.yoha();// error: Cannot invoke an object which is possibly 'undefined'.}}}
Expected behavior:
bar should behave the same as this.bar since they are indentical and passed by the simplest assignment operation.
Actual behavior:
Type inference for bar seems to break down after being initialized as the indentical of this.bar, shown as the demonstration code.
This was marked as a duplicate of #19955 which was actually related to a particular case of using if statements in loops. That was fixed, yet this problem remails.
#27706 seems to be the same problem but it is treated as specific case when the argument/variable is initially of type unknown.
TypeScript Version: 3.1.0-dev.20180824
Search Terms: Type Inference, Union Type, Assignment Operation
Code
Expected behavior:
bar
should behave the same asthis.bar
since they are indentical and passed by the simplest assignment operation.Actual behavior:
Type inference for
bar
seems to break down after being initialized as the indentical ofthis.bar
, shown as the demonstration code.Playground Link: http://www.typescriptlang.org/play/#src=interface%20BarA%20%7B%0D%0A%20%20label%3A%20true%3B%0D%0A%20%20yoha%3F()%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20BarB%20%7B%0D%0A%20%20label%3A%20false%3B%0D%0A%20%20yoha%3F()%3A%20void%3B%0D%0A%7D%0D%0A%0D%0Aclass%20Foo%20%7B%0D%0A%20%20bar!%3A%20BarA%20%7C%20BarB%3B%0D%0A%0D%0A%20%20pia()%3A%20void%20%7B%0D%0A%20%20%20%20if%20(this.bar.yoha)%20%7B%0D%0A%20%20%20%20%20%20this.bar.yoha()%3B%20%2F%2F%20worked%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20if%20(this.bar.yoha)%20%7B%0D%0A%20%20%20%20%20%20if%20(this.bar.label)%20%7B%0D%0A%20%20%20%20%20%20%20%20let%20label%20%3D%20this.bar.yoha()%3B%20%2F%2F%20worked%0D%0A%20%20%20%20%20%20%7D%20else%20%7B%0D%0A%20%20%20%20%20%20%20%20this.bar.yoha()%3B%20%2F%2F%20worked%0D%0A%20%20%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20%20%20this.bar.yoha()%3B%20%2F%2F%20worked%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20let%20bar%20%3D%20this.bar%3B%0D%0A%0D%0A%20%20%20%20if%20(bar.yoha)%20%7B%0D%0A%20%20%20%20%20%20bar.yoha()%3B%20%2F%2F%20worked%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20if%20(bar.yoha)%20%7B%0D%0A%20%20%20%20%20%20if%20(bar.label)%20%7B%0D%0A%20%20%20%20%20%20%20%20let%20label%20%3D%20bar.yoha()%3B%20%2F%2F%20failed%0D%0A%20%20%20%20%20%20%7D%20else%20%7B%0D%0A%20%20%20%20%20%20%20%20bar.yoha()%3B%20%2F%2F%20failed%0D%0A%20%20%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20%20%20bar.yoha()%3B%20%2F%2F%20failed%0D%0A%20%20%20%20%7D%0D%0A%20%20%7D%0D%0A%7D
Related Issues: not yet
The text was updated successfully, but these errors were encountered: