简体   繁体   中英

How to infer type for arguments for function?

I have a type declaration like this to exclude the return union type:

export type ExcludeReturnType<T extends (...args: any[]) => any, R> = (...args: any) => Exclude<ReturnType<T>, R>

type Orig = (a: number) => string | void
type Result = ExcludeReturnType<Orig, void> // (...args: any[]) => string
type Expected = (a: number) => string

But right now it will just turn all parameters into any[] . How can I preserve the type of the original function?

Playground

Use the Parameters utility type:

export type ExcludeReturnType<T extends (...args: any[]) => any, R> = 
  (...args: Parameters<T>) => Exclude<ReturnType<T>, R>
type Orig = (a: number, b: string) => string | void
type Result = ExcludeReturnType<Orig, void> 
// type Result = (a: number, b: string) => string

Playground

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