TypeScript获取泛型中键的类型

4
我有一个名为“Store”的类,它将状态类型作为泛型获取。
class Store<T> {

}

当我需要扩展商店时,我会像这样做:
interface State {
   entities: { [id: string] : Todo }
}

class Todos extends Store<State> {

}

但现在我想在我的存储库中添加一个方法,以返回实体。
class Store<T> {

  private _store: BehaviorSubject<T>;

  constructor(initialState) {
    super();
    this._store = new BehaviorSubject(initialState);
  }

  getEntities() {
     return this._store.map(state => state.entities);
  }      
}

在这种情况下,我该如何将getEntities的返回类型定义为Todo类型?


这是rxjs的事情,与此无关。 - undefined
getEntities(): Observable<T>? - Explosion Pills
1个回答

2
您可以像在类声明中定义泛型类型一样定义 getEntities 的泛型返回类型:
getEntities() : BehaviorSubject<T> {
   return this._store.map(state => state.entities);
}

T是整个状态类型,我需要实体键的类型。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接