Get Argument types for Function/Constructor in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 27, 2024
3 min

banner

# Table of Contents

  1. Get the Type of a Function's Arguments in TypeScript
  2. Get the Parameters type of a Class Constructor in TS

# Get the Type of a Function's Arguments in TypeScript

Use the Parameters utility type to get the type of a function's arguments.

The Parameters utility type constructs a tuple type from the types used in the function's parameters.

index.ts
function sum(a: number, b: string): string { return a + b; } // ๐Ÿ‘‡๏ธ type SumParams = [a: number, b: string] type SumParams = Parameters<typeof sum>; // ๐Ÿ‘‡๏ธ type FirstParam = number type FirstParam = SumParams[0]; // ๐Ÿ‘‡๏ธ type SecondParam = string type SecondParam = SumParams[1];
The code for this article is available on GitHub

The Parameters utility type constructs a tuple type from the types used in the function's parameters.

You can access the type of a specific parameter by using square brackets, in the same way, that you would access an array element at an index.

Here is an example of how you would use the Parameters utility type for 2 functions that take the same object as a parameter.

index.ts
type Person = { name: string; age: number; country: string; }; function getObj(obj: Person): Person { return obj; } function getObj2(obj: Parameters<typeof getObj>[0]) { return obj; } console.log(getObj2({ name: 'Bobby Hadz', age: 30, country: 'Chile' }));

using parameters utility type for 2 functions

The getObj2() function takes an object of the same type as the one the getObj() function takes.

After we get the tuple of parameter types, we have to access the element at index 0 to get the type of the object.

index.ts
type Person = { name: string; age: number; country: string; }; function getObj(obj: Person): Person { return obj; } // ๐Ÿ‘‡๏ธ type GetObjParams = [obj: Person] type GetObjParams = Parameters<typeof getObj>

This is because the Parameters utility type returns a tuple, even if the function takes a single parameter.

# Get the Parameters type of a Class Constructor in TS

Use the ConstructorParameters utility type to get the parameters type of a class constructor in TypeScript.

index.ts
class Person { constructor(public name: string, public age: number, public country: string) { this.name = name; this.age = age; this.country = country; } } // ๐Ÿ‘‡๏ธ type PersonParamsType = [name: string, age: number, country: string] type PersonParamsType = ConstructorParameters<typeof Person>; // ๐Ÿ‘‡๏ธ type First = string type First = PersonParamsType[0]; // ๐Ÿ‘‡๏ธ type Second = number type Second = PersonParamsType[1];
The code for this article is available on GitHub

We used the ConstructorParameters utility type to get a tuple type of all of the constructor function's parameter types.

If you need to access the type of a specific, parameter, e.g. the first one, you can use bracket notation and access the element at the specific index.

index.ts
class Person { constructor(public name: string, public age: number, public country: string) { this.name = name; this.age = age; this.country = country; } } // ๐Ÿ‘‡๏ธ type PersonParamsType = [name: string, age: number, country: string] type PersonParamsType = ConstructorParameters<typeof Person>; // ๐Ÿ‘‡๏ธ type First = string type First = PersonParamsType[0]; // ๐Ÿ‘‡๏ธ type Second = number type Second = PersonParamsType[1]; // ๐Ÿ‘‡๏ธ type Third = string type Third = PersonParamsType[2];

The indexes of the tuple are zero-based, just like with arrays.

Note that the ConstructorParameters utility type would return a tuple containing the parameter types, even if the constructor function takes a single parameter.

index.ts
class Person { name: string; age: number; country: string; constructor({ name, age, country, }: { name: string; age: number; country: string; }) { this.name = name; this.age = age; this.country = country; } } // ๐Ÿ‘‡๏ธ type PersonParamsType = [{ // name: string; // age: number; // country: string; // }] type PersonParamsType = ConstructorParameters<typeof Person>; // ๐Ÿ‘‡๏ธ type First = { // name: string; // age: number; // country: string; // } type First = PersonParamsType[0];

The class in the example takes a single parameter - an object. However, the ConstructorParameters still returns a tuple containing the object.

If you need to access the type of the object, you need to access the tuple element at index 0.

If you need to pass a function as a parameter, check out the following article.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev