【发布时间】:2022-01-17 15:34:39
【问题描述】:
我有这样的代码 -
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();
}
它工作得很好,但是 Typescript 抱怨它在 State1 类中找不到静态方法handlaA()。
TS2339: Property 'handleA' does not exist on type 'StateTypes'. Property 'handleA' does not exist on type 'State1'.
【问题讨论】:
-
抱歉,打错了。现在更正它。
标签: javascript typescript static-methods