如何正确公开重载实现签名?
基于这个问题:
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 [];
}
Run Code Online (Sandbox Code Playgroud)
如何包装此函数但根据选项参数保留可能的返回类型?
例如:
function wrapFunction(arg1 arg2, options) {
// do something with arg1 …Run Code Online (Sandbox Code Playgroud)