I am attempting to define a function that uses generics in the following way:
public fetch<M extends Model, T extends Service<M> = Service<M>>(service: string): T {
return this.services[service]<M, T>();
}
where Service requires a generic M extends Model. Now, T is an extension of Service that has a generic that extends Model, for instance UserService<UserModel>.
The above works but question is, is there a way to define the above function fetch so that I do not have to specify M explicitly, for instance:
// This is obviously bogus but I think it gets my point across.
public fetch<T extends Service<GenericOf<T>>>(service: string): T {
return this.services[service]<GenericOf<T>, T>();
}
I have looked around and imagined it would be a Utility Type of sort that would allow me to get the generic used in a type. So would I be able to define an operator that extracts the generic of the type provided to it type M = GenericOf<UserService>?