Here is a code snippet that showcases the use of state handling in TypeScript:
type StateTypes = State1 | State2;
class State1 {
static handleA (): StateTypes {
// Do Something
return State2;
}
static handleB (): StateTypes {
// Do Something
return State1;
}
}
class State2 {
static handleA (): StateTypes {
// Do Something
return State1;
}
static handleB (): StateTypes {
// Do Something
return State2;
}
}
let currentState: StateTypes = State1;
for (/* some Condition*/){
if(/* some Condition*/)
currentState = currentState.handleA();
else
currentState = currentState.handleB();
}
The code executes without any errors but Typescript raises an issue stating that it cannot find the static method 'handlaA()' in class State1.
TS2339: Property 'handleA' does not exist on type 'StateTypes'. Property 'handleA' does not exist on type 'State1'.