在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
答案 0 :(得分:3)
看起来像您想要的那样:
type FirstArgumentType<T> = T extends (arg1: infer U, ...args: any[]) => any ? U : never;