上下文:我想开发一个模式,使用TypeState库在打字稿中创建可扩展状态机。 TypeState为Typescript提供了一个类型安全的状态机,虽然不是问题的核心,但它有助于说明我的目标。创建可扩展枚举用于可扩展接口
问题:我遇到了在打字稿扩展enum
和interface
和class
声明实现它们创造一个可扩展的模式问题。
目标:下面的psuedocode说明了我想让我的模式看起来像什么。
1)定义基enum States
2)扩展enum States
与附加状态导致enum ExtendedStates
2)使用States
定义ParentInterface
和输入状态机
3)经由ChildInterface
扩展ParentInterface
并重写States
与ExtendedStates
4)实施ParentInterface
在class Parent
5)扩展class Parent
在class Child
实施ChildInterface
6)能够调用broadcastState()
从任一类,并获得当前的状态。
我已经在其他语言中使用了这种模式,对于理解Typescript的局限性和任何可以实现相同目标的替代模式我都会很感激。
import {TypeState} from "typestate";
enum States {
InitialState
}
// extends is not available on enum, looking for alternative
enum ExtendedStates extends States {
AdditionalState
}
/////////////////////////////////////////
// this works fine
interface ParentInterface {
fsm: TypeState.FiniteStateMachine<States>;
states: typeof States;
message: string;
}
// incorrectly extends ParentInterface, types of fsm/states are incompatible
interface ChildInterface extends ParentInterface {
fsm: TypeState.FiniteStateMachine<ExtendedStates>;
states: typeof ExtendedStates;
}
/////////////////////////////////////////
class Parent implements ParentInterface {
public fsm: TypeState.FiniteStateMachine<States>;
public states: typeof States;
public message: string = "The current state is: ";
constructor(state: States | undefined) {
state = state ? state : this.states.InitialState;
this.fsm = new TypeState.FiniteStateMachine(state);
this.broadcastCurrentState();
}
public broadcastCurrentState(): void {
console.log(this.message + this.fsm.currentState);
}
}
class Child extends Parent implements ChildInterface {
public fsm: TypeState.FiniteStateMachine<ExtendedStates>;
public states: typeof ExtendedStates;
constructor(state: ExtendedStates | undefined) {
state = state ? state : this.states.InitialState;
this.fsm = new TypeState.FiniteStateMachine(ExtendedStates);
this.broadcastCurrentState();
}
}
最近我已经得到
import {TypeState} from "typestate";
enum States {
InitialState
}
enum ExtendedStates {
InitialState,
ExtendedState
}
class Parent {
public fsm: TypeState.FiniteStateMachine<States>;
public states: typeof States;
public message: string = "The current state is: ";
// T is declared but never used
constructor(state: <T> | undefined) {
state = state ? state : this.states.InitialState;
// cannot find name T
this.fsm = new TypeState.FiniteStateMachine<T>(state);
this.broadcastCurrentState();
}
public broadcastCurrentState(): void {
console.log(this.message + this.fsm.currentState);
}
}
// types of fsm are incompatible
class Child extends Parent {
public fsm: TypeState.FiniteStateMachine<ExtendedStates>;
public states: typeof ExtendedStates;
constructor(state: ExtendedStates | undefined) {
// Param not assignable to type <T>
super(state);
}
}
这种尝试得到接近理想的结果,但在enum
很多重复的代码不编译和结果。它也失去了界面,这不是要求,但提供了一个很好的安全网。
我很想听听你们都说了些什么。我觉得这是一种强大的模式,我错过了一些简单的事情来实现它。
伟大的答案,我认为这将工作得很好。 – gjolund