簡體   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