Issue
How do I correctly expose an overloaded implementation signature?
Scenario
Expanding on the initial query:
interface MyMap<T> {
[id: string]: T;
}
type Options = {
asObject?: boolean,
other?: Function
testing?: number
};
function get(options: { asObject: true, other?: Function, testing?:number }): MyMap<any>;
function get(options: { asObject?: false, other?: Function, testing?:number }): number[];
function get(): any[];
function get(options: Options = { asObject: false }): any[] | MyMap<any> {
if (options?.asObject) return {} as MyMap<any>;
return [];
}
How can I wrap this function while preserving the potential return types based on the options argument?
For instance:
function wrapFunction(arg1 arg2, options) {
// perform actions with arg1 and arg2
return get(options)
}
Based on the options provided in the wrapFunction method, the return type will be determined by the return type of get for that specific value.
e.g.:
const g = wrapFunction(1,2, {asObject: true})
// The result should match get({asObject:true})
Potential Solutions
I could create a new signature for each wrapFunction, but this would become lengthy, especially when dealing with multiple types of wrapFunctions following the same pattern of nested get
calls.
One recommendation was to cast the wrapFunction as typeof get
, but this limits the ability to modify the parameter list of wrapFunction.
You may find this relevant.