如何使用解构数组作为参数调用泛型方法

How to call a generic method with a deconstruct array as it's parameter

我需要调用一个以解构数组作为参数的泛型方法

据我所知,目前唯一的安全类型继承参数方法是使用解构数组并强制其类型 然后调用父方法,使用应用原型

例如:

此 GET 承诺请求

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

    }
  }

假设我将需要 相同的方法,但作为一个可观察的

in my actual case, there are multiple methods with specific parameters, that would be chaotic to setup as static parameter types, since it would change as the project requirements shift. so i needed to use the parameter array, if you have a better solution for this declaration, please feel free to send it

认为的方式:

public getObservable<T>(...params: Parameters<HttpService['getAsync']>): Observable<T> {
    const request = this.getAsync<T>.apply(this,params);
    return from(request);
}

但事实并非如此:

所以我想知道,是否有一种方法可以使用 .apply 调用泛型方法?

也许可以尝试 bind 方法:

    public getObservable<T>(...params: Parameters<HttpService['getAsync']>): Observable<T> {
        const request = this.getAsync.bind(this);
        return from(request<T>(...params));
    }