Encountering the following error message:
TS2365: Operator '===' cannot be applied to types 'ExampleState.Unsaved' and 'ExampleState.Saving'.
While trying to compare an enum with a mutable member variable:
enum ExampleState {
Unset = -1,
Unsaved = 0,
Saving = 1,
Saved = 2
}
class Example {
private state : ExampleState = ExampleState.Unset;
public Save() {
if (this.state === ExampleState.Unsaved) {
this.BeginSaving();
while (this.state === ExampleState.Saving) { // !error!
this.CommitSave();
}
}
}
private BeginSaving() {
this.state = ExampleState.Saving;
}
private CommitSave() {
this.state = ExampleState.Saved;
}
}
The scenario in question involves an asynchronous function that makes multiple save attempts - however, it has been simplified here for clarity.
It seems that Typescript is not recognizing that the variable can be changed and is making assumptions incorrectly. What could be causing this issue and how can it be resolved?