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
functiontest(){typeYesNo=('No'|'Yes');letanswer: YesNo='No';// Bug disappears after adding 'as YesNo'.try{answer='Yes';return;// Bug disappears after removing the return.}finally{if(answer==='Yes'){// Bug: Operator === cannot be applied to types 'No' and 'Yes'}elseif(answer==='No'){}}}
Expected behavior:
No compilation errors.
Actual behavior:
Compilation error:
bug.ts(8,13): error TS2365: Operator '===' cannot be applied to types '"No"' and '"Yes"'.
Note:
This bug is similar to "Erroneous control flow type narrowing in loops": #15835
Reacted by SlurpTheo, Mattias Buelens, Aliaksei Tuzik, Edmundo Santos, Daniel Tiziani and Antoine Latter
Also facing this while setup a DB connection and trying to finalize it under the finally block if it was created.
letconnection;try{connection=awaitgetConnection();const{ rows }=awaitconnection.query(sql,values);returncamelizeKeys(rows);}finally{if(connection){// [ts] Property 'end' does not exist on type 'never'. [2339]awaitconnection.end();}}
I'm facing the SAME issue, trying to finalize a db connection.
What I could figure out is that it happends when a function returns anything at all, it presents the error, but if it's a void function, the issue is not present.
I can't reproduce the earlier problems in this issue anymore (using typescript@3.7.4), but I do get the same CFA problem described by Daniel Tiziani (@dtiziani) in the findById()-example.
Commenting-out either the return or the throw statement prevents the problem.
Now, if you slightly tweak the example (to use undefined instead of null), there's another interesting bit that not explicitly initializing to = undefined prevents the problem as well:
functionfindById2(id: string){// not explicitly initializing this to `= undefined` prevents the problemletconnection: string|undefined=undefined;try{connection=id;// commenting-out this `return` -OR- the `throw` below prevents the problemreturnconnection;}catch(e){console.log(e);// commenting-out this `throw` -OR- the `return` above prevents the problemthrowe;}finally{if(connection){// ts(2339) -// Property 'length' does not exist on type 'never'.console.log(connection.length);}}}
And this "work-around" reminds me of this CFA conversation... only there's no function expression involved.
Activity
rdsedmundo commentedon Feb 6, 2019
Also facing this while setup a DB connection and trying to finalize it under the
finallyblock if it was created.dtiziani commentedon Dec 12, 2019
I'm facing the SAME issue, trying to finalize a db connection.
What I could figure out is that it happends when a function returns anything at all, it presents the error, but if it's a void function, the issue is not present.
Here's a playground with both scenarios: http://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=29&pc=4#code/FAMwrgdgxgLglgewgAhHCATAQgTwHICGAtgKYAUExJAXMgM4wBO6A5gJS0BuCcGyA3sGTDkAGxIxkUJBBKxEEWg2YQWyAD7IIYUaOQBeLTtEBuYEJFMcAiyKky58JAa1UzdgL5SCMKAAtkMhI2Gzs7aQg6BHEAOlEEFiC2WzCYP0YEAHdkEncRLzRKXWtBMOQ4EDII2XlnAEJDbV0QiKjY+MTqxwU4klU05M8LD3NQSFqUQuwcAEkMMl4lJlYOemXVDSNdUJFxSS6JpZU1TSa9RuMzFKsd8IcJl14rsuFGCTBGFAOnCDzhLygPn8gWCtzCrWiJDiCSSKTsaQy2VyKQK6AIxTBwgqVXuP2QDS2ohaSDaUI6OIgNR+vX6fkG+WGQA
also the code below:
SlurpTheo commentedon Jan 9, 2020
I can't reproduce the earlier problems in this issue anymore (using
typescript@3.7.4), but I do get the same CFA problem described by Daniel Tiziani (@dtiziani) in thefindById()-example.Commenting-out either the
returnor thethrowstatement prevents the problem.Now, if you slightly tweak the example (to use
undefinedinstead ofnull), there's another interesting bit that not explicitly initializing to= undefinedprevents the problem as well:And this "work-around" reminds me of this CFA conversation... only there's no function expression involved.
RyanCavanaugh commentedon Jan 9, 2020
SlurpTheo fixed in nightly; see #34880