Javascript 如何在Typescript中按名称调用具有不同签名的方法?
我正在尝试建立一个方法来调用我的后端并为我做一般的事情 因此,我得出以下代码:Javascript 如何在Typescript中按名称调用具有不同签名的方法?,javascript,angular,algorithm,typescript,angular-http,Javascript,Angular,Algorithm,Typescript,Angular Http,我正在尝试建立一个方法来调用我的后端并为我做一般的事情 因此,我得出以下代码: myFct() { this[type](url, params, options).pipe(...) } get<T>(url: string, params: HttpParams, options: any): Observable<HttpEvent<T>> { return this._http.get<T>(url, options);
myFct() {
this[type](url, params, options).pipe(...)
}
get<T>(url: string, params: HttpParams, options: any): Observable<HttpEvent<T>> {
return this._http.get<T>(url, options);
}
post<T>(url: string, params: HttpParams, options: any): Observable<HttpEvent<T>> {
return this._http.post<T>(url, params, options);
}
put<T>(url: string, params: HttpParams, options: any): Observable<HttpEvent<T>> {
return this._http.put<T>(url, params, options);
}
delete<T>(url: string, params: HttpParams, options: any): Observable<HttpEvent<T>> {
return this._http.delete<T>(url, options);
}
但我得到了这个错误:
Cannot invoke an expression whose type lacks a call signature
因为GET和DELETE的签名中没有HttpParams
它们是我根据条件返回参数的一种方式吗?或者返回两个参数并与签名匹配?您可以使用以下事实,即返回所需的参数数,并执行以下操作:
(this._http[type].length === 3 ?
this._http[type](url, params, options) :
this._http[type](url, options));
您可以组合捕获类型、声明合并和映射类型的帮助器,以达到所需的效果。不幸的是
type Method = 'post' | 'put' | 'delete' | 'get';
type R = {
[M in Method]: (this: { http: HttpClient }, method: M) => typeof this['http'][M]
};
interface MyHttp extends R { }
class MyHttp {
constructor(http: HttpClient) {
Object.assign(
this, (<Method[]>['get', 'post', 'put', 'delete'])
.map(method => http[method].bind(http))
);
}
}
type方法='post'|'put'|'delete'|'get';
R型={
[M在方法中]:(this:{http:HttpClient},方法:M)=>typeof this['http'][M]
};
接口MyHttp扩展R{}
类MyHttp{
构造函数(http:HttpClient){
Object.assign(
这,(['get','post','put','delete'])
.map(方法=>http[method].bind(http))
);
}
}
我相信使用函数重载可以得到您想要的:
myFct<T>(type: "get" | "delete", url: string, options: any): Observable<HttpEvent<T>>;
myFct<T>(type: "post" | "put", url: string, params: HttpParams, options: any): Observable<HttpEvent<T>>;
myFct<T>(type: "get" | "delete" | "post" | "put", url: string, paramsOrOptions: HttpParams | any, options?: any): Observable<HttpEvent<T>> {
return type == "get" || type == "delete"
? this._http[type](url, paramsOrOptions)
: this._http[type](url, paramsOrOptions, options)
}
http.myFct<any>("get", "http://", {}) // OK
http.myFct<any>("get", "http://", {}, {}) // Error (too many args)
http.myFct<any>("post", "http://", {}, {}) // OK
http.myFct<any>("post", "http://", {}) // Error (too few args)
myFct(类型:“get”|“delete”,url:string,options:any):可观察;
myFct(类型:“post”|“put”,url:string,params:HttpParams,options:any):可观察;
myFct(类型:“get”|“delete”|“post”|“put”,url:string,paramsOrOptions:HttpParams | any,options?:any):可观察{
返回类型==“获取”| |类型==“删除”
?此._http[类型](url,参数选项)
:this._http[type](url、参数选项、选项)
}
myFct(“get”,“http://”,“{})//确定
myFct(“get”,“http://”,“{},{})//错误(参数太多)
myFct(“post”,“http://”,“{},{})//OK
myFct(“post”,“http://”,“{})//错误(参数太少)
尽管就个人而言,我不确定这是否真的比只有4个函数好。不幸的是,除非OP合并到
HttpClient
中每个方法的length
属性的类型声明中(或者仅仅是类型断言),type
从何而来?你能举一个简单的例子来说明这个错误吗?它是一个字符串,可以是“get”、“put”、“post”或“delete”,那么它不应该是一个错误,因为你所有的函数都有3个参数。。。编辑:哎呀,我没有看到调用this的更改。\u http
而不是this
。。。现在有道理了
myFct<T>(type: "get" | "delete", url: string, options: any): Observable<HttpEvent<T>>;
myFct<T>(type: "post" | "put", url: string, params: HttpParams, options: any): Observable<HttpEvent<T>>;
myFct<T>(type: "get" | "delete" | "post" | "put", url: string, paramsOrOptions: HttpParams | any, options?: any): Observable<HttpEvent<T>> {
return type == "get" || type == "delete"
? this._http[type](url, paramsOrOptions)
: this._http[type](url, paramsOrOptions, options)
}
http.myFct<any>("get", "http://", {}) // OK
http.myFct<any>("get", "http://", {}, {}) // Error (too many args)
http.myFct<any>("post", "http://", {}, {}) // OK
http.myFct<any>("post", "http://", {}) // Error (too few args)