简体   繁体   中英

TypeScript: Something similar to ReturnType<…> needed but for the type of the first function argument

Is it possible in TypeScript to declare something like ReturnType<...> that does not retrieve the type of the return value but the type of the first argument instead?

type SingleArgFunction<A, R> = (arg: A) => R

// wrong => how to implement the following line correctly?
type FirstArgType<T extends SingleArgFunction<infer A, any>> = A 

const numberToString: SingleArgFunction<number, string> =
    (x: number) => String(x)

type R = ReturnType<typeof numberToString> // R => string

type F = FirstArgType<typeof numberToString> // F => number

看起来这可以满足您的要求:

type FirstArgumentType<T> = T extends (arg1: infer U, ...args: any[]) => any ? U : never;

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