TypeScript:需要类似于ReturnType <...>的东西,但第一个函数参数的类型

时间:2018-10-08 09:14:44

标签: typescript typescript-typings

在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

1 个答案:

答案 0 :(得分:3)

看起来像您想要的那样:

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