The ConstructorParameters type helps you get the list of input types a class constructor needs. It makes working with classes easier and safer.
ConstructorParameters type in Typescript
ConstructorParameters<Type>
Type must be a class constructor type.
The result is a tuple type representing the constructor's parameter types.
Person as a tuple type.class Person { constructor(name: string, age: number) {} } type PersonParams = ConstructorParameters<typeof Person>; // PersonParams is [string, number]
class Car { constructor(make: string, year?: number) {} } type CarParams = ConstructorParameters<typeof Car>; // CarParams is [string, (number | undefined)?]
ConstructorParameters with any constructor type, not just classes.type Params = ConstructorParameters<new (x: boolean) => any>; // Params is [boolean]
This program uses ConstructorParameters to get the constructor argument types of Book. Then it creates a new Book instance safely with those arguments.
class Book { constructor(title: string, pages: number) { this.title = title; this.pages = pages; } title: string; pages: number; } function createInstance<T extends new (...args: any) => any>(ctor: T, args: ConstructorParameters<T>) { return new ctor(...args); } const bookArgs: ConstructorParameters<typeof Book> = ["Learn TypeScript", 250]; const myBook = createInstance(Book, bookArgs); console.log(myBook.title); console.log(myBook.pages);
If the constructor has no parameters, ConstructorParameters returns an empty tuple [].
This type only works with class constructors or constructor signatures, not regular functions.
ConstructorParameters extracts the types of a class constructor's parameters as a tuple.
It helps write safer and more flexible code when working with class instances.
Use it when you want to forward or reuse constructor argument types.
Practice
What does the ConstructorParameters type utility do in TypeScript?
Solution
Step 1: Understand the purpose of ConstructorParameters
TheConstructorParametersutility extracts the types of the parameters used in a class constructor and returns them as a tuple type.Step 2: Compare with other options
Options B, C, and D describe different behaviors thatConstructorParametersdoes not perform.Final Answer:
Extracts the types of a class constructor's parameters as a tuple -> Option AQuick Check:
ConstructorParameters = tuple of constructor args [OK]
- Confusing ConstructorParameters with ReturnType
- Thinking it creates a new class
- Assuming it returns a union type
Which of the following is the correct syntax to get the constructor parameter types of a class MyClass using ConstructorParameters?
class MyClass { constructor(name: string, age: number) {} }Solution
Step 1: Identify the correct type argument for ConstructorParameters
The argument must be a constructor function type, which istypeof MyClass(the class constructor type), not the instance type.Step 2: Check each option
type Params = ConstructorParameters<MyClass>; uses the instance type, which is incorrect. type Params = ConstructorParameters; uses a non-existent property. type Params = ConstructorParameters; uses invalid syntax. Only type Params = ConstructorParameters; correctly usestypeof MyClass.Final Answer:
type Params = ConstructorParameters<typeof MyClass>; -> Option BQuick Check:
Use typeof ClassName with ConstructorParameters [OK]
- Passing instance type instead of constructor type
- Using invalid syntax like new expressions
- Trying to access constructor property
What is the output type of the following code?
class Person {
constructor(public name: string, public age: number) {}
}
type Params = ConstructorParameters<typeof Person>;Solution
Step 1: Analyze the constructor parameters
The constructor takes two parameters:nameof typestringandageof typenumber.Step 2: Understand ConstructorParameters output
ConstructorParameters<typeof Person>returns a tuple of the parameter types:[string, number].Final Answer:
[string, number] -> Option CQuick Check:
ConstructorParameters returns tuple of constructor arg types [OK]
- Expecting parameter names in the tuple
- Confusing parameter types with instance properties
- Thinking it returns an error
Identify the error in this code snippet:
class Car {
constructor(make: string, year: number) {}
}
type Params = ConstructorParameters<Car>;Solution
Step 1: Check the argument passed to ConstructorParameters
The argument must be the constructor function type, which istypeof Car, not the instance typeCar.Step 2: Identify the error
UsingCardirectly causes a type error because it is the instance type, not the constructor type.Final Answer:
Car is an instance type, should use typeof Car -> Option AQuick Check:
Use typeof ClassName with ConstructorParameters [OK]
- Passing instance type instead of constructor type
- Assuming ConstructorParameters works on instances
- Adding parentheses after class name
Given the following classes, how can you create a type alias EmployeeParams that matches the constructor parameters of Employee by reusing the constructor parameters of Person?
class Person {
constructor(public name: string, public age: number) {}
}
class Employee extends Person {
constructor(name: string, age: number, public salary: number) {
super(name, age);
}
}Solution
Step 1: Understand the constructor parameters
Personconstructor takesnameandage.Employeeaddssalaryas a third parameter.Step 2: Reuse Person's constructor parameters and add salary
UsingConstructorParameters<typeof Person>gives[string, number]. Addingnumberfor salary creates[string, number, number].Step 3: Check options
type EmployeeParams = [...ConstructorParameters<typeof Person>, number]; correctly uses tuple spread to combine parameters. type EmployeeParams = ConstructorParameters<typeof Employee>; gets all parameters but does not reuse Person's. type EmployeeParams = ConstructorParameters<Person> & number; is invalid syntax. type EmployeeParams = [string, number, number]; hardcodes types without reuse.Final Answer:
type EmployeeParams = [...ConstructorParameters<typeof Person>, number]; -> Option DQuick Check:
Reuse constructor params with tuple spread [OK]
- Not using typeof with class name
- Hardcoding parameter types instead of reusing
- Using invalid intersection (&) instead of tuple spread
Ready to prove it?
Pass the test and this pattern moves unknown
