[英]How to copy parameters and return type of function?
I have function x
and I have function y
.我有函数x
并且我有函数y
。 How can I make function y
have all the same parameter and return types as function x
?如何使函数y
具有与函数x
相同的参数和返回类型? Essentially I'd like to make them identical, except for their inner workings (everything in between {
and }
).本质上,我想让它们相同,除了它们的内部工作原理( {
和}
之间的所有内容)。
I know I can use Parameters<typeof x>
and ReturnType<typeof x>
but was wondering if there was a single utility that does everything in one hit.我知道我可以使用Parameters<typeof x>
和ReturnType<typeof x>
但想知道是否有一个实用程序可以一键完成所有操作。
typeof x
will give you the full type: 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.