Typescript static method does not exist on type someclass
I have code like this -
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();
}
It works perfectly fine, however Typescript complains 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'.
type StateTypes = State1 | State2
means instance of State1
or State2
.
What you want is: type StateTypes = typeof State1 | typeof State2
. This refers to constructors instead of instances