简体   繁体   中英

How to copy parameters and return type of function?

I have function x and I have function y . How can I make function y have all the same parameter and return types as function 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.

typeof x will give you the full type:

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' }

Playground link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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