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
enum States {
State1,
State2,
State3
}
class TypeComparisonProblem {
private num: number
constructor () {
this.num = 0
}
public operation (): void {
if (this.state === States.State1) {
this.num++ // If previous num was 0 the state will now be State2 when checked on next row
if (this.state === States.State2) { // This lines throws: error TS2365: Operator '===' cannot be applied to types 'States.State1' and 'States.State2'.
this.num--
}
}
}
get state (): States {
if (this.num < 1) return States.State1
if (this.num < 10) return States.State2
return States.State3
}
}
Expected behavior:
The above class should compile without errors.
Actual behavior: TS2365: Operator '===' cannot be applied to types 'States.State1' and 'States.State2'.
It assumes that the "state" is "State1" due to the comparison (this.state === States.State1) but as the state is computed depending on the private "num" property it could have an other value when accessed the second time.
The text was updated successfully, but these errors were encountered:
See #9998.
I don't think this case is something that we would ever handle as you expect though, since getters that let one property affect another tend to be rare. We wouldn't want to be assuming that any assignment anywhere could change the type of properties or control flow analysis wouldn't be nearly as useful.
Note that the example will work if state() is a method because we don't narrow the types of method results.
TypeScript Version: 2.7.0-dev.20171101
Expected behavior:
The above class should compile without errors.
Actual behavior:
TS2365: Operator '===' cannot be applied to types 'States.State1' and 'States.State2'.
It assumes that the "state" is "State1" due to the comparison (this.state === States.State1) but as the state is computed depending on the private "num" property it could have an other value when accessed the second time.
The text was updated successfully, but these errors were encountered: