TypeScript:如何委托接口方法
我有这样一个来自第三方库的界面:TypeScript:如何委托接口方法,typescript,proxy-pattern,Typescript,Proxy Pattern,我有这样一个来自第三方库的界面: MyInterface{ foo(arg: string): Promise<any>; foo(arg: string, ...params: any[]): Promise<any>; foo<T>(arg: string): Promise<T>; foo<T>(arg: string, ...params: any[]): Promise<T>;
MyInterface{
foo(arg: string): Promise<any>;
foo(arg: string, ...params: any[]): Promise<any>;
foo<T>(arg: string): Promise<T>;
foo<T>(arg: string, ...params: any[]): Promise<T>;
bar(arg: string, callback?: (err: Error, row: any) => void): Promise<number>;
bar(arg: string, ...params: any[]): Promise<number>;
}
MyInterface{
foo(arg:string):承诺
也没有
正在帮助我进行正确的授权。不是最好的解决方案,但仍有一些解决方法:
接口MyInterface{
foo(arg:string):string
foo(arg:number):number
}
类Foo实现MyInterface{
foo(arg:string):字符串;
foo(arg:number):数字;
//foo(arg:number):string;->错误,如果您取消对此行的注释,请对上面的行进行注释
foo(arg:string | number){
如果(参数类型=='string'){
返回参数
}
返回12作为数字
}
}
const result=new Foo().Foo(2)
坏消息:您应该编写所有重载。好了,不可能用重载映射接口
好消息是:如果您提供的函数重载与实现的接口不兼容,TS将投诉
更新
还有另一种方法
您可能不需要通过MyInterface
扩展Foo
类
接口MyInterface{
foo(arg:string):string
foo(arg:number):number
}
福班{
foo(arg:string):字符串;
foo(arg:number):数字;
foo(arg:string | number){
如果(参数类型=='string'){
返回参数
}
返回12作为数字
}
}
//很抱歉,我找不到指向此类型实用程序作者的链接
深度相等类型=
(()=>T扩展X?1:2)扩展
(()=>T扩展Y?1:2)?真:假;
/**
*在这里,您可以检查类型是否完全相等
*尝试更改'foo'方法的重载,然后
*将看到结果类型为falsy
*/
L型=深
当方法调用应该委托给类似的方法签名时,委托的方法可以决定如何处理给定的参数
重载方法签名可以归结为最通用的重载(与静态类型语言有些不同)。因此,不需要实现接口中声明的更具体的方法,但可以将声明作为编译器提示包含在实现接口的类中。在这种情况下,aka vararg参数(…arg:any[]
)非常方便,因为它提供了泛型方法签名
class MyClass implements MyInterface {
constructor(private delegate:MyInterface){}
// these 2 methods below can't be declared in this class since they have
// the same signature as the ones with <T> type argument (type-erasure?)
// foo(arg: string, ...params: any[]): Promise<any>;
// foo(arg: string): Promise<any>;
//compiler hint, the method below will be called at runtime
foo<T>(arg: string): Promise<T>;
foo<T>(arg: string, ...params: any[]): Promise<T>{
//we leave the responsibility to handle the given arguments to the `delegate` implementation
return this.delegate.foo(arg, ...params);
}
// this method declaration has no implicit implementation, at runtime
// the more generic method below will be called, this method acts
// as a compiler "hint" and can be ommitted
bar(arg: string, callback?: (err: Error, row: any) => void): Promise<number>;
bar(arg, ...params: any[]): Promise<number>{
return this.delegate.bar(arg,params);
}
}
类MyClass实现MyInterface{
构造函数(私有委托:MyInterface){}
//以下两个方法无法在此类中声明,因为它们已
//与具有类型参数的签名相同(类型擦除?)
//foo(arg:string,…params:any[]):Promise;
//foo(arg:string):承诺;
//编译器提示,下面的方法将在运行时调用
foo(arg:string):承诺;
foo(arg:string,…params:any[]):Promise{
//我们将处理给定参数的责任留给“委托”实现
返回this.delegate.foo(arg,…params);
}
//此方法声明在运行时没有隐式实现
//将调用下面更通用的方法,此方法起作用
//作为编译器的“提示”,可以进行编辑
条(arg:string,callback?:(err:Error,row:any)=>void):承诺;
bar(arg,…params:any[]):承诺{
返回此.delegate.bar(arg,params);
}
}
你说两者都没有帮助是什么意思?当你使用他们的答案时会发生什么?请不要删除其中一个。我无法将给出的答案应用到我的用例中,可能是应用到有限的知识中。我删除了另一个,也没有人能回答…你尝试过什么吗?你似乎发布了完全相同的问题两个小时前你有过这样的经历,你没有从他们那里学到什么吗?
class MyClass implements MyInterface {
constructor(private delegate:MyInterface){}
// these 2 methods below can't be declared in this class since they have
// the same signature as the ones with <T> type argument (type-erasure?)
// foo(arg: string, ...params: any[]): Promise<any>;
// foo(arg: string): Promise<any>;
//compiler hint, the method below will be called at runtime
foo<T>(arg: string): Promise<T>;
foo<T>(arg: string, ...params: any[]): Promise<T>{
//we leave the responsibility to handle the given arguments to the `delegate` implementation
return this.delegate.foo(arg, ...params);
}
// this method declaration has no implicit implementation, at runtime
// the more generic method below will be called, this method acts
// as a compiler "hint" and can be ommitted
bar(arg: string, callback?: (err: Error, row: any) => void): Promise<number>;
bar(arg, ...params: any[]): Promise<number>{
return this.delegate.bar(arg,params);
}
}