[英]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.