I have an interface like this coming from an 3rd party lib:
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>;
}
And I want to delegate the interface methods to an implementation of the same type, like this:
MyClass implements MyInterface {
private impl:MyInterface = ...
foo(..) //how to do it right ??
// TS2393: Duplicate function implementation.
bar(arg: string, callback?: (err: Error, row: any) => void): Promise<number>{
return impl.bar(sql,callback);
}
// TS2393: Duplicate function implementation.
bar(arg, ...params: any[]): Promise<number>{
return impl.bar(arg,params);
}
}
I have no idea how to implement the delegation correctly, so the right impl
methods are called.
Neither TypeScript function overloading Nor Is there a way to do method overloading in TypeScript? is helping me to make the correct delegation.
When method calls should be delegate to similar methods signatures, the delegated method can decide how to handle the given parameters.
The overloaded method signatures boil down to the most generic overload (somewhat different to static typed languages). Therefore the more specific methods declared in the interface are not required to be implemented, but there declaration can be included in the class that implements the interface as a compiler hint. In this case the rest aka vararg parameter (
...arg:any[]
) comes in handy since it renders a generic method signature.