Get the Parameters type of a Class Constructor in TS

avatar

Borislav Hadzhiev

Last updated: Mar 12, 2022

banner

Photo from Unsplash

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, e.g. type T = ConstructorParameters<typeof MyClass>. The ConstructorParameters type returns a tuple type containing the parameter types of the constructor function.

index.ts
// ✅ For constructors of classes 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]; // ✅ For regular functions function sum(a: number, b: number): number { return a + b; } // 👇️ type SumParamsType = [a: number, b: number] type SumParamsType = Parameters<typeof sum>;

We used th 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 get the type of a regular function's parameters, you'd use the Parameters utility type instead.

index.ts
function sum(a: number, b: number): number { return a + b; } // 👇️ type SumParamsType = [a: number, b: number] type SumParamsType = Parameters<typeof sum>; // 👇️ type First = number type First = SumParamsType[0]; // 👇️ type Second = number type Second = SumParamsType[1];

The Parameters utility type also returns a tuple type containing the types of all of the function's parameters.

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.