我想创建一个Map,该Map的类类型为键,而该类的实例为值。
到目前为止,我已经能够生成这个:
export interface RequestState
{
new(): PrimaryState<RequestData>;
}
//create a new map that uses classtype as the key and instance as the value.
private states: Map<RequestState, PrimaryState<RequestData>> = new Map<RequestState, PrimaryState<RequestData>>();
//set a new instance of the state_requestRoom class.
this.states.set(state_requestRoom, new state_requestRoom());
一切正常,但是,现在我希望能够将该实例作为通用类型获取:
public activateState<T extends RequestState>(): T
{
const state: PrimaryState<RequestData> = this.states.get(T);
return state;
}
但是这给了我错误'T'仅指类型,但是在这里被用作值。ts(2693) 我可以将参数作为参数传递,但是据我所知,我只能将实例作为PrimaryState返回。我想执行以下操作:
const requestRoomState: state_requestRoom = stateHandler.activateState<state_requestRoom>();