Currently, I am creating a custom method that wraps around Angular's HttpClient service. I want users to have the ability to pass in options, but I am struggling to find the proper way to reference that type in my method parameter definition.
For example:
constructor(private http: HttpClient) {}
my_request<T = any>(endpoint: string, payload: T, options: WhatTypeHere = {}) {
return this.http.post<MyHttpResponse>(this.build_url(endpoint), this.build_payload(payload), {
...options,
withCredentials: true,
});
}
In the options
parameter of my_request()
, I am unsure of what to specify in order to enforce it when using the method.
Here is the definition of HttpClient.post()
for reference:
post(url: string, body: any | null, options?: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}): Observable<Object>;
I know that I could simply copy and paste the entire definition into my method, but I am looking for a more concise way to reference it, especially if definitions change in the future.
One approach I attempted was something like
options: HttpClient['post']['options']
, but it did not work as expected.
Is there an alternative method I can use to reference that type?