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
The following example doesn't compile without errors in the most recent TypeScript nightly build. Control flow analysis determines that scanner.current has the type "/", and thus TypeScript flags the string comparison with "*". However, it doesn't take into account that current is a getter property, which returns a different value after advance() has been called.
Wouldn't it make sense to treat getter properties like methods in this case and not narrow their type to a string literal type?
Code
classScanner{constructor(privateinput: string,privatepos=0){}advance(){this.pos++;}getcurrent(){returnthis.input[this.pos];}}constscanner=newScanner("/* Comment */");if(scanner.current==="/"){scanner.advance();if(scanner.current==="*"){// Error// Operator '===' cannot be applied to types '"/"' and '"*"'}}
Expected behavior:
No errors.
Actual behavior:
Error Operator '===' cannot be applied to types '"/"' and '"*"'
The text was updated successfully, but these errors were encountered:
This is not really related to the property being computed or even to its being a property at all. Calling any function could have arbitrary side effects that cannot plausibly be tracked. Here is a minimal repro
letcurrent="/* comment */";constadvance=()=>{current=current.slice(1);};if(current==="/"){advance();if(current==="*"){// [ts] Operator '===' cannot be applied to types '"/"' and '"*"'}}
Thank your for linking to #9998. For interested readers, Anders precisely answers my questions here. Interestingly, the same issue came up in the TypeScript compiler (advancing to the next token).
tl;dr: It's a calculated tradeoff of control flow analysis.
TypeScript Version: 2.0.3 / nightly (2.1.0-dev.20161106)
The following example doesn't compile without errors in the most recent TypeScript nightly build. Control flow analysis determines that
scanner.current
has the type"/"
, and thus TypeScript flags the string comparison with"*"
. However, it doesn't take into account thatcurrent
is a getter property, which returns a different value afteradvance()
has been called.Wouldn't it make sense to treat getter properties like methods in this case and not narrow their type to a string literal type?
Code
Expected behavior:
No errors.
Actual behavior:
Error
Operator '===' cannot be applied to types '"/"' and '"*"'
The text was updated successfully, but these errors were encountered: