Hey,
I'm trying to implement a method that match the following :
test() {
this.get<ArrayBuffer>('/test'); // NOT OK: expect error -> expected 2 arguments, got 1 (responseType is missing)
this.get<ArrayBuffer>('/test', 'arraybuffer'); // OK
this.get<Blob>('/test'); // NOT OK: expect error -> expected 2 arguments, got 1 (responseType is missing)
this.get<Blob>('/test', 'blob'); // OK
this.get<string>('/test'); // NOT OK: expect error -> expected 2 arguments, got 1 (responseType is missing)
this.get<string>('/test', 'text'); // OK
this.get<{ test: string }>('/test', 'json'); // OK
this.get<{ test: string }>('/test'); // OK (responseType is optional, defaults to 'json')
}
public get<R, T = R extends ResponseTypeValues ? never : 'json'>(
url: string,
responseType?: T,
): Observable<R>;
public get<R, T = R extends ResponseTypeValues ? ResponseTypeKey<R> : never>(
url: string,
responseType: T,
): Observable<R>;
public get(url: string, responseType?: 'json' | ResponseTypeKeys): Observable<unknown> {
return this.http.get(url, { headers: this.DEFAULT_HEADERS, responseType: (responseType ?? 'json') as 'json' });
}
Is there anything I do wrongly ? 🤔
(I'm on typescript 5.1)