Calling a Generic Method with Deconstructed Array Parameters
As of today, the only method to ensure typed safe inherited parameters is by using a deconstructed array and explicitly defining its type. This allows calling the parent method by utilizing the apply prototype.
For instance:
If we consider this GET promise request
public async getAsync<T>(url: string, options: HttpOptions<'GET'>): Promise<T> {
try {
const response = await this.http.get(url, options.params, options.headers);
return response.data;
} catch (e) {
throw new Error(e);
}
}
Imagine needing the same method but as an observable
In my current scenario, there exist multiple methods with distinct parameters that would become overwhelming if set as static parameter types due to shifting project requirements. Hence, resorting to the use of a parameter array was necessary. If you have alternative solutions for this declaration, suggestions are welcome.
The way I envisioned it going:
public getObservable<T>(...params: Parameters<HttpService['getAsync']>): Observable<T> {
const request = this.getAsync<T>.apply(this,params);
return from(request);
}
However, it did not proceed as expected:
https://i.sstatic.net/EymXk.png
Hence, my inquiry revolves around whether there exists a method to utilize .apply when calling a generic method?