Is there ArgumentsType<T> like ReturnType<T> in Typescript?

Edit

Since writing the original answer, typescript now has a built-in type (defined in lib.d.ts) to get the type of the parameters called Parameters

type argsEmpty = Parameters<() => void> // []
type args = Parameters<(x: number, y: string, z: boolean) => void> // [number, string, boolean]
type argsOpt = Parameters<(x: number, y?: string, z?: boolean) => void> // [number, (string | undefined)?, (boolean | undefined)?]

Edit Typescript 3.0 has been relesed the code below works as expected.

While this is not possible in the current version of typescript (2.9) without spelling out all parameters. It will become possible in the next version of typescript (3.0) which will be released in the next few days:

type ArgumentsType<T> = T extends  (...args: infer U) => any ? U: never;

type argsEmpty = ArgumentsType<() => void> // []
type args = ArgumentsType<(x: number, y: string, z: boolean) => void> // [number, string, boolean]
type argsOpt = ArgumentsType<(x: number, y?: string, z?: boolean) => void> // [number, (string | undefined)?, (boolean | undefined)?]

If you install npm install typescript@next you can already play with this, it should be available sometime this month.

Note

We can also spread a tuple into arguments with this new feature:

type Spread<T extends any[]> = (...args: T)=> void;
type Func = Spread<args> //(x: number, y: string, z: boolean) => void

You can read more about this feature here


As of TypeScript 3.1, Parameters type is now a part of the standard library.

type Result = Parameters<(a: number, b: string) => boolean>; // [number, string]

There is no way for now to extract both the types and amount of arguments for any function possible. But you can try something like this:

type ArgumentTypes<T> = T extends () => any ? never[] :
                        T extends (a1: infer T1) => any ? [T1] :
                        T extends (a1: infer T1, a2: infer T2) => any ? [T1, T2] :
                        // continue here for any reasonable number of args
                        never;

Check it with the following:

const args0: ArgumentTypes<() => boolean> = []; // correct
const args1: ArgumentTypes<(a: number) => boolean> = [1]; // correct
const args2: ArgumentTypes<(a: number, b: string) => boolean> = [1, 'str']; // correct

const oops0null: ArgumentTypes<() => boolean> = null; // error, arguments are array (but empty one)
const oops01: ArgumentTypes<() => boolean> = [1]; // error, arguments must be empty

const oops10: ArgumentTypes<(a: number) => boolean> = []; // error, we need one argument
const oops12: ArgumentTypes<(a: number) => boolean> = [1, 2]; // error, we need only one argument
const oops1wrong: ArgumentTypes<(a: number) => boolean> = ['str']; // error, argument must be number

const oops21: ArgumentTypes<(a: number, b: string) => boolean> = [1]; // error, we need two arguments
const oops23: ArgumentTypes<(a: number, b: string) => boolean> = [1, 'str', undefined]; // error, we need only two arguments
const oops2wrong: ArgumentTypes<(a: number, b: string) => boolean> = ['str', 1]; // error, arguments are reversed

Note that this don't have any use of optional arguments - they are just omitted from the output. I wasn't able to find a way to catch them for now.

Tags:

Typescript