How to get the parameters type of a Class Constructor in TypeScript

In this tutorial, we will show you the instruction on how to get the parameters type of a Class Constructor. The ConstructorParameters utility type can help us to do this in TypeScript. Check out the information below. We will walk through some examples to give you a better understanding.

How to get the Parameters type of a Class Constructor

Typescript has the ConstructorParameters built-in utility type that can help you get the parameters type of a Class Constructor

This utility type allows us to construct a tuple or array type from the types of a constructor type.

To illustrate, take a look at this example:

// For constructors of classes
class Student {
	constructor(
		public name: string,
		public id: string,
		public className: string
	) {
		this.name = name;
		this.id = id;
		this.className = className;
	}
}

// Get the parameters type of a Class Constructor
type StudentParamsType = ConstructorParameters<typeof Student>;

The ConstructorParameters utility type produces a tuple type with all the parameter types. Therefore, when hovering your mouse at the StudentParamsType type, you can see the following result:

If you want to access the type of a specific parameter, you can access the element by its corresponding index using the bracket notation. For instance:

class Student {
	constructor(
		public name: string,
		public id: string,
		public className: string
	) {
		this.name = name;
		this.id = id;
		this.className = className;
	}
}

// Get the parameters type of a Class Constructor
type StudentParamsType = ConstructorParameters<typeof Student>;

// Access the first parameter type: type firstType = string
type firstType = StudentParamsType[0];

// Access the second parameter type: type secondType = string
type secondType = StudentParamsType[1];

// Access the third parameter type: type thirdType = string
type thirdType = StudentParamsType[2];

The index in a tuple starts at zero. So when accessing the first element, we need to access the element with index ‘0’ in the tuple.

One more important note is that when using the ConstructorParameters utility type, you must ensure that you pass in a class and not an instance of the class. Otherwise, you will get an error message as in the example below:

class Student {
	constructor(
		public name: string,
		public id: string,
		public className: string
	) {
		this.name = name;
		this.id = id;
		this.className = className;
	}
}

const newStudent = new Student("James", "S_22862", "IT_01");

// Pass in an instance of the Student class
type StudentParamsType = ConstructorParameters<typeof newStudent>;

Error:

Type 'Student' does not satisfy the constraint 'abstract new (...args: any) => any'.
Type 'Student' provides no match for the signature 'new (...args: any): any'.

Summary

To sum up, TypeScript provides the ConstructorParameters built-in utility type that can help you get the parameters type of a Class Constructor. That’s all we want to convey through this article. Hopefully, through the examples in this tutorial, you have grasped the method we are referring to.

Leave a Reply

Your email address will not be published. Required fields are marked *