Reputation: 1531
Consider the following setup:
genericModel.ts
export default class GenericModel<TModelId> {
id!: TModelId;
constructor(defaultId: any){
this.id = defaultId;
}
}
genericStore.ts
class GenericStore<TModel extends GenericModel<any>> {
@observable loadedModels = observable.map<typeof(TModel.id), TModel>();
constructor(service: TService){
this.service = service;
}
}
export default GenericStore;
I know that you cannot call typeof on a generic and why -- this is only to illustrate intent. The observable decorator is from mobx, but it is just a unique structure that requires the type of the model id to use as an identifier, and the model type itself. Now I know I can make it work like this:
class GenericStore<TModelId, TModel extends GenericModel<TModelId, any>, TService> {
@observable loadedModels = observable.map<TModelId, TModel>();
constructor(service: TService){
this.service = service;
}
}
export default GenericStore;
But when I am extending the generic store, the type of the model is already declared, so I am thinking there should be a way to infer it without having to declare the TModelId explicitly. I'm new to generics so this may just not be possible but if that's the case I just want confirmation.
Upvotes: 0
Views: 27
Reputation: 374
I think TModel["id"]
is what you're looking for. Complete example:
class GenericModel<ID> {
constructor(public id: ID) { }
}
class GenericStore<TModel extends GenericModel<any>> {
public aaa!: TModel["id"];
}
const store1 = new GenericStore<GenericModel<number>>();
store1.aaa; // number
Upvotes: 1