Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

372
Views
Duplicate function implementation in Typescript

I am writing an adapter for the HttpClient from Angular and I need two distinct get functions. One that returns a Blob and one that returns a generic. But when I try to implement it, I get the error:

TS2393: Duplicate function implementation.

  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));
  }

I don't understand that this is an error when the actual implementation of the get functions in the HttpClient class looks almost the same.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You cannot have two functions with the same name (and within the same scope) in javascript. You must instead make a single function that can work out which arguments you have and do the right thing, then with typescript you can write two different function signatures to get the types correct e.g.:

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
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!