Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

typescript

ReturnType<T> extracts return type of a function.

Is there a way to define ArgumentsType<T> that extracts parameter types of a function in tuple format?

For example,

ArgumentsType<(a: number, b: string) => boolean> will be [number, string].

like image 940
Joon Avatar asked Jul 12 '18 07:07

Joon


People also ask

How do you pass a callback function in TypeScript?

Similar to JavaScript, to pass a function as a parameter in TypeScript, define a function expecting a parameter that will receive the callback function, then trigger the callback function inside the parent function.

What is call signature in TypeScript?

In TypeScript we can express its type as: ( a : number , b : number ) => number. This is TypeScript's syntax for a function's type, or call signature (also called a type signature). You'll notice it looks remarkably similar to an arrow function—this is intentional!

How do you find the return type of a function in TypeScript?

Use the ReturnType utility type to get the return type of a function in TypeScript, e.g. type T = ReturnType<typeof myFunction> . The ReturnType utility type constructs a type that consists of the return type of the provided function type.

Does TypeScript have optional?

TypeScript provides a Optional parameters feature. By using Optional parameters featuers, we can declare some paramters in the function optional, so that client need not required to pass value to optional parameters.


2 Answers

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

like image 199
Titian Cernicova-Dragomir Avatar answered Sep 19 '22 17:09

Titian Cernicova-Dragomir


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] 
like image 25
Karol Majewski Avatar answered Sep 18 '22 17:09

Karol Majewski