[Typescript] Infer first argument of a function

type OneArgFn<A = any> = (firstArg: A, ..._args: any[]) => void
type GetFirstArg<T>
    = T extends OneArgFn<infer R>
        ? R
        : never;

// Test case
function foo(x: string, y: number) {return null}
//        ^?
type t1 = GetFirstArg<typeof foo>
//    ^? string

 

posted @ 2024-01-29 23:39  Zhentiw  阅读(2)  评论(0编辑  收藏  举报