prmph
prmph

Reputation: 8176

Inferring arbitrary function arguments as object or tuple type in TypeScript

I'm looking for something like this type:

type ArgsType<F extends Function> = ...

where

ArgsType<(n: number, s: string)=>void>

will give me

[number, string]

or

{n: number, s: string}

Based on one of the answers, I created these types:

type ArgsType<F extends (...x: any[]) => any>
    = F extends (...x: infer A) => any ? A : never;

type CtorArgsType<F extends new (...x: any[]) => any>
    = F extends new (...x: infer A) => any ? A : never;


interface RepoGroup {
    resetAsync?: () => Promise<void>
}

interface RepoGroupOptions<Reset extends "CanReset" | "CannotReset"="CannotReset"> {
    reset: Reset extends "CanReset" ? () => Promise<void> : undefined
}

type RepoGroupCtor<Reset extends "CanReset" | "CannotReset"="CannotReset">
    = new (...args: any[]) => RepoGroupOptions<Reset>


export function generate<
    CanReset extends "CanReset" | "CannotReset"="CannotReset",
    T extends RepoGroupCtor<CanReset>=RepoGroupCtor<CanReset>
    >(args: T) {
    return class implements RepoGroup {
        private args: InstanceType<T>
        constructor(...config: CtorArgsType<T>) {
            this.args = new args(config) as any
        }

        resetAsync = this.args.reset
    }
}


export const Repo = generate(class {
    readonly baseUrl: string
    constructor(args: { apiBaseUrl: string }) {
        this.baseUrl = args.apiBaseUrl
    }

    reset: undefined
})

let repository = new Repo()

The last line shows an error, as it should. But if I add just a generic parameter to the repo thus:

export const Repo = generate<"CannotReset">(class {...

then the error disappears, which seems like a bug

Upvotes: 2

Views: 2545

Answers (2)

c69
c69

Reputation: 21497

You can use Parameters<T>. It is available since (at least) 3.2

Usage:


function log(s: string) { console.log(s)}
type LogArg = Parameters<typeof log>[0]; // string

interface Logger {
 log(s: string) => void;
}
type LogArg2 = Parameters<Logger['log']>[0]; // string

Upvotes: 2

Matt McCutchen
Matt McCutchen

Reputation: 30889

type Parameters<T extends (...args: any[]) => any> = T extends (...args: infer P) => any ? P : never;

which is proposed to be added to the standard library.

Round 2

When you pass at least one type argument to generate, you shut off inference for the remaining type arguments and the defaults are used (T = RepoGroupCtor<CanReset>), and RepoGroupCtor<CanReset> accepts a rest parameter of any[], hence there is no error. Partial type argument inference would give you a way to avoid this.

Upvotes: 3

Related Questions