繁体   English   中英

如何复制函数的参数和返回类型?

[英]How to copy parameters and return type of function?

我有函数x并且我有函数y 如何使函数y具有与函数x相同的参数和返回类型? 本质上,我想让它们相同,除了它们的内部工作原理( {}之间的所有内容)。

我知道我可以使用Parameters<typeof x>ReturnType<typeof x>但想知道是否有一个实用程序可以一键完成所有操作。

typeof x会给你完整的类型:

const x = (a: string, b: number): boolean => { return true }

const y: typeof x = (a: string, b: number): boolean => { return false }

// leaving the individual types out, but they're still string, number, boolean
const implicitY: typeof x = (a, b) => { return false } 

// This misuse of `typeof x` shows typescript errors
const badY: typeof x = (a: number, b: string): boolean => { return 'hi' }

游乐场链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM