0

I'm trying to use inheritance on HttpClient and override all its common methods

but Typescript is pointing an error when I try to override the request method

is there an explanation about error pointing out by Typescript

because it didnt have error when I override GET/POST/PUT/DELETE/OPTIONS

this only 'request' method only like this

enter image description here

2
  • Take a look at your declaration of the options attribute, and compare that to the base class. Commented Sep 28, 2020 at 11:47
  • Yep. done comparing it to the base class and I believe those parameter in my screenshot is the right parameters. about the option parameter, I intentionally set its type to 'any' so I could pass the parameters without having trouble in the lint since its type is an anonymous object Commented Sep 28, 2020 at 12:27

2 Answers 2

1

This runs through ng build

@Injectable()
export class FoobarHttpClient extends HttpClient {

    request( method: string, url: string, options?: {
        body?: any;
        headers?: HttpHeaders | {
            [header: string]: string | string[];
        };
        params?: HttpParams | {
            [param: string]: string | string[];
        };
        observe?: 'body' | 'events' | 'response'; // == HttpObserve;
        reportProgress?: boolean;
        responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';
        withCredentials?: boolean;
    } ): Observable<any> {
        return super.request( method, url, options );
    }

}

My project is still on Angular 9, and there http.d.ts doesn't expose the internally used HttpObserve (maybe that's now exported in Angular 10, I didn't check).

If you haven't done so already, I recommend looking into the ng HttpInterceptor. Using an interceptor is the common approach to customize HttpClient-behavior.

Sign up to request clarification or add additional context in comments.

2 Comments

I did something like this approach but it still pointing the same thing on my intellisense. could you provide some link on how I could achieve the same thing using Interceptor?
0

This is only hack but I couldn't think of a proper way to solve this

so just by declaring the 'request' method in this way, I could remove that error:

request: any = (method: string, url: string, options?: any): Observable<any> => {

    if (this.authService.config.activateInterception) {
        return this.requestWithRetry(method, url, 0, options);
    }
    else {
        return super.request(method, url, options);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.