[英]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? 是否可以在TypeScript中声明类似ReturnType <...>的内容,该内容不检索返回值的类型,而是检索第一个参数的类型?
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.