Estoy escribiendo un adaptador para HttpClient de Angular y necesito dos funciones de obtención distintas. Uno que devuelve un Blob y otro que devuelve un genérico. Pero cuando trato de implementarlo, me sale el error:
TS2393: Implementación de función duplicada.
get<T>(url: string, options?: { headers?: HttpHeaders | { [header: string]: string | string[]; }; context?: HttpContext; observe?: 'body'; params?: HttpParams | { [param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>; }; reportProgress?: boolean; responseType?: 'json'; withCredentials?: boolean; }): Observable<T> { return this.handleRequest(this.http.get<T>(url, options)); } get(url: string, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: 'response'; context?: HttpContext; params?: HttpParams | { [param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>; }; reportProgress?: boolean; responseType: 'blob'; withCredentials?: boolean; }): Observable<HttpResponse<Blob>> { return this.handleRequest(this.http.get(url, options)); }No entiendo que esto sea un error cuando la implementación real de las funciones get en la clase HttpClient se ve casi igual.
No puede tener dos funciones con el mismo nombre (y dentro del mismo alcance) en javascript. En su lugar, debe crear una sola función que pueda determinar qué argumentos tiene y hacer lo correcto, luego, con mecanografiado, puede escribir dos firmas de funciones diferentes para obtener los tipos correctos, por ejemplo:
get<T>(url: string, observe: 'body'): Observable<T>; get(url: string, observe: 'response'): Observable<HttpResponse<Blob>>; get(url: string, observe: 'body' | 'response') { if (observe === 'body') { // ... implementation } else { // ... implementation } }