如何制作函数的参数'类型遵循另一个函数的参数'
type Func = (...args: any) => any;
// I use overwrite() to re-define an existing function
function overwrite(originalFunc: Func, newFunc: Func) {
}
function demo(str: string, num: number) {
console.log(str, num);
}
// Re-define the demo() function
overwrite(demo, (str, num) {
console.log('new func', str, num);
demo(str, num);
});
如上所述,在打字稿中,如何定义overwrite()定义的类型以使 newfunc 的参数遵循 oinderfunc 的参数。
type Func = (...args: any) => any;
// I use overwrite() to re-define an existing function
function overwrite(originalFunc: Func, newFunc: Func) {
}
function demo(str: string, num: number) {
console.log(str, num);
}
// Re-define the demo() function
overwrite(demo, (str, num) {
console.log('new func', str, num);
demo(str, num);
});
As above, in TypeScript how to define the types in definition of overwrite() to make the newFunc's arguments follow the originalFunc's.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用仿制药来定义两个参数共享的函数类型
Use generics to define a function type that both parameters share