ConstructorParameters<T>
Extracts the parameter types of a constructor function type T as a tuple type.
Definition
type ConstructorParameters<T extends abstract new (...args: any) => any> =
T extends abstract new (...args: infer P) => any ? P : never;Examples
class DatabaseConnection {
constructor(
public host: string,
public port: number,
public database: string,
) {}
}
type DBArgs = ConstructorParameters<typeof DatabaseConnection>;
// [host: string, port: number, database: string]
const args: DBArgs = ["localhost", 5432, "mydb"];class Logger {
constructor(public prefix: string, public level: "info" | "warn" | "error") {}
log(msg: string) { console.log(`[${this.prefix}] ${msg}`); }
}
function createInstance<T extends new (...args: any[]) => any>(
Cls: T,
...args: ConstructorParameters<T>
): InstanceType<T> {
return new Cls(...args);
}
const logger = createInstance(Logger, "App", "info");abstract class Shape {
constructor(public color: string, public filled: boolean) {}
abstract area(): number;
}
type ShapeArgs = ConstructorParameters<typeof Shape>;
// [color: string, filled: boolean]
class Circle extends Shape {
constructor(color: string, filled: boolean, public radius: number) {
super(color, filled);
}
area() { return Math.PI * this.radius ** 2; }
}Common Use Cases
- 1Building type-safe factory functions
- 2Creating dependency injection containers
- 3Deriving constructor args for test fixtures
- 4Abstracting instantiation in plugin systems
- 5Forwarding constructor arguments in wrapper classes
Understanding ConstructorParameters<T>
ConstructorParameters<T> extracts the parameter types from a class constructor and returns them as a tuple. It is the constructor-specific counterpart to Parameters, which works on regular function types.
This utility type uses the abstract new (...args: any) => any constraint, which means it works with both concrete and abstract classes. The infer keyword captures the constructor's parameter list within the conditional type expression.
The most common use case is building type-safe factory functions. A generic factory that accepts a class and its constructor arguments can use ConstructorParameters<T> to ensure the arguments match the class's constructor signature. Combined with InstanceType<T> for the return type, you get a fully type-safe factory pattern.
Dependency injection containers benefit greatly from this type. When registering a class with a DI container, you can use ConstructorParameters to describe the dependencies that need to be resolved. The container can then verify at the type level that all dependencies are satisfiable.
In testing, ConstructorParameters helps create fixture factories. Instead of hardcoding constructor arguments for test data, you can build generic helpers that accept ConstructorParameters<typeof MyClass> and create instances with default or randomized values.
Note that ConstructorParameters requires typeof when used with a class name. The class name alone refers to the instance type, while typeof ClassName refers to the constructor function—which is what ConstructorParameters needs to extract parameters from.
Related Types
Parameters<T>Extracts the parameter types of a function type T as a tuple, enabling you to reuse a function's argument types elsewhere.
InstanceType<T>Extracts the instance type of a constructor function type T, giving you the type of objects created by the constructor.
ReturnType<T>Extracts the return type of a function type T, enabling you to reuse a function's output type without manual duplication.
infer keywordThe infer keyword declares a type variable within a conditional type's extends clause, extracting and capturing part of a type for use in the true branch.
More Object & Function Types
Explore TypeScript Types
Browse our complete reference of 30 TypeScript utility types with definitions, examples, and explanations.