Course contentsShow
← TypeScript
Lesson 1456 of 1,79633. Inference Inversion PatternsFree lesson

Extracting Constructor Parameter Types

Use ConstructorParameters to get parameter types from class constructors and new signatures.

Extracting Constructor Parameter Types

What you'll learn: How to use the ConstructorParameters utility type to extract the parameter types from class constructors and new signatures, giving you type-safe access to what arguments a class needs to be instantiated.


Why Extract Constructor Parameters?

Sometimes you need to work with the types of arguments required to construct a class, without actually instantiating it. Perhaps you're building a factory function, a dependency injection container, or a serialization utility that needs to know what parameters a constructor expects.

TypeScript's ConstructorParameters<T> utility type solves this by extracting constructor parameter types as a tuple type.


Basic Usage with Classes

When you have a class, ConstructorParameters gives you a tuple type representing its constructor parameters:

class User {
  constructor(public name: string, public age: number, public active: boolean = true) {}
}

// Extract the constructor parameters as a tuple
type UserParams = ConstructorParameters<typeof User>;
// Result: [name: string, age: number, active?: boolean]

// Now you can use this type elsewhere
const params: UserParams = ["Alice", 30, false];
const user = new User(...params); // Type-safe spreading

Notice we use typeof User, not just User. We need the constructor function type, not the instance type.


With Constructor Signatures

ConstructorParameters also works with explicit constructor type signatures:

type PersonConstructor = new (name: string, age: number) => Person;

type PersonParams = ConstructorParameters<PersonConstructor>;
// Result: [name: string, age: number]

This is particularly useful when working with abstract constructor patterns or when a constructor type is passed as a parameter.


Real-World Pattern: Generic Factory

Here's where this becomes powerful — building type-safe factory functions:

function createInstances<T extends new (...args: any[]) => any>(
  Constructor: T,
  count: number,
  ...args: ConstructorParameters<T>
): InstanceType<T>[] {
  return Array.from({ length: count }, () => new Constructor(...args));
}

class Product {
  constructor(public name: string, public price: number) {}
}

// Fully type-safe: you must provide correct constructor arguments
const products = createInstances(Product, 3, "Widget", 9.99);
// products is Product[]

The factory knows exactly what arguments Product needs because ConstructorParameters<T> extracted them.


Combining with Other Utilities

You can combine ConstructorParameters with other extraction utilities you've learned:

class Database {
  constructor(
    public host: string,
    public port: number,
    public credentials?: { user: string; password: string }
  ) {}
}

type DbParams = ConstructorParameters<typeof Database>;
// [string, number, { user: string; password: string } | undefined]

// Extract just the first parameter type
type HostType = DbParams[0]; // string

// Extract all required parameters (filter out optional)
type RequiredDbParams = Required<DbParams>;

Key Takeaway: ConstructorParameters<T> extracts constructor parameter types as a tuple, enabling type-safe factory functions, dependency injection patterns, and any code that needs to work with constructor arguments without hardcoding their types.