Consider the following type
export type SomeToken<K extends string, V = any> = {
[key in K]: V;
} & {
created_at: Date;
created_by_user: string;
};
usually K is a single string (e.g. K = 'token_a'). These tokens are loaded with the following method
public getToken<TokenId extends string>(token_id: string): Observable<SomeToken<TokenId>> {
return this.http.get<SomeToken<TokenId>>(ENDPOINT_URL + '/' + token_id);
}
To call getToken I need to do something like getToken<'token_id'>('token_id'). token_id is repeated twice. Is there a way to avoid it?
token_idhave the generic type instead ofstring, likepublic getToken<T extends string>(token_id: T): Observable<SomeToken<T>> {}. If that meets your needs I could write up an answer explaining; if not, what am I missing?