[英]How to define the return type of a function according to the input parameters with Typescript?
[英]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.