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
enumETest{A,B,C}letg: ETest;functiontest_a(): void{g=ETest.B;}(async()=>{g=ETest.A;awaitnewPromise(function(resolve,reject): void{setTimeout(resolve,1000);});if(g===ETest.B){// here will be marked as an errorconsole.log("hey");}})();test_a();
Expected behavior:
That line shouldn't be marked as an error.
Actual behavior:
TS-compiler shows an error like this:
error TS2365: Operator '===' cannot be applied to types 'ETest.A' and 'ETest.B'.
16:33:07 - Compilation complete. Watching for file changes.
Maybe the strict type checking should be disabled for closured variables or this.xxxx?
The text was updated successfully, but these errors were encountered:
This is related to how control flow analysis handles async/generator functions. They need special handling such as #11928.
However, code blocks in async/generator function are handled as if they were synchronous. This is pretty sweet if variable g is confined in async function, that is, g is only mutated inside async function, but it also breaks your code. We can also handle async/generator function as truly "async". But it breaks other inferences. For similar discussion, see #9998. I believe there is no silver bullet.
In this particular example, I would suggest finding a workaround which avoids changing g. Or, you can write a get_g function.
enumETest{A,B,C}letg: ETest;functiontest_a(): void{g=ETest.B;}functionget_g(){returng}(async()=>{g=ETest.A;awaitnewPromise(function(resolve,reject): void{setTimeout(resolve,1000);});if(get_g()===ETest.B){// here will be marked as an errorconsole.log("hey");}})();test_a();
TypeScript Version: 2.2.2 / nightly (2.2.0-dev.201xxxxx)
Code
Expected behavior:
That line shouldn't be marked as an error.
Actual behavior:
TS-compiler shows an error like this:
Maybe the strict type checking should be disabled for closured variables or
this.xxxx
?The text was updated successfully, but these errors were encountered: