Skip to main content

What does ConstructorParameters<T> do?

What ConstructorParameters<T> does

ConstructorParameters<T> is a built-in TypeScript utility type that extracts the constructor's parameter types from a given class or constructor.

It works just like Parameters<T>, but for classes (or constructor functions).


Definition

In the TypeScript source:

javascript
type ConstructorParameters<T extends abstract new (...args: any) => any> = T extends abstract new (...args: infer P) => any ? P : never;

Breakdown:

  • T is the constructor type, that is, something like new (...args) => Instance.
  • infer P extracts the constructor's argument types.
  • The result is a tuple of the constructor's parameter types.

Code examples

1. A regular class

javascript
class User { constructor(public name: string, public age: number) {} } type UserParams = ConstructorParameters<typeof User>; // [string, number] const args: UserParams = ["Alex", 30]; const user = new User(...args); // correct

2. A class without a constructor

javascript
class Empty {} type P = ConstructorParameters<typeof Empty>; // []

3. An abstract class

javascript
abstract class Base { constructor(protected id: number, public name: string) {} } type BaseParams = ConstructorParameters<typeof Base>; // [number, string]

4. A constructor as a type

javascript
type Ctor = new (flag: boolean, count: number) => object; type Args = ConstructorParameters<Ctor>; // [boolean, number]

What happens if you pass a non-constructor

If you pass something that is not a constructor (for example, a regular function or a type without new), TypeScript raises an error:

Type 'T' does not satisfy the constraint 'abstract new (...args: any) => any'

Example:

javascript
type Bad = ConstructorParameters<() => void>; // Error: the function is not a constructor

Practical applications

1. Dynamically creating instances

javascript
function createInstance<T extends new (...args: any[]) => any>( ctor: T, ...args: ConstructorParameters<T> ): InstanceType<T> { return new ctor(...args); } class Person { constructor(public name: string, public age: number) {} } const p = createInstance(Person, "Tom", 25); // correctly typed

2. Using it in DI / factories

javascript
function makeSingleton<T extends new (...args: any[]) => any>(Ctor: T) { let instance: InstanceType<T> | null = null; return (...args: ConstructorParameters<T>): InstanceType<T> => { if (!instance) instance = new Ctor(...args); return instance; }; } class Config { constructor(public host: string, public port: number) {} } const getConfig = makeSingleton(Config); const c1 = getConfig("localhost", 8080); const c2 = getConfig("localhost", 8080); // the same instance

3. Used together with InstanceType<T>

  • ConstructorParameters<T> gives the constructor's arguments
  • InstanceType<T> gives the type of the created instance
javascript
class Point { constructor(public x: number, public y: number) {} } type Args = ConstructorParameters<typeof Point>; // [number, number] type Instance = InstanceType<typeof Point>; // Point

Summary

QuestionAnswer
What does ConstructorParameters<T> do?Extracts the parameter types of class T's constructor as a tuple.
What can be passed?Only constructors (classes or types of the form new (...args) => any).
What happens with a non-constructor?A type error.
What does it return?A tuple of the constructor's argument types.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.