Codectionary / Developer documentation / TypeScript

ConstructorParameters and InstanceType

ConstructorParameters extracts a constructor’s argument tuple, while InstanceType extracts the type of its instances. They are useful for factories and wrappers that should stay aligned with a class signature. ConstructorParameters requires TypeScript 3.1+; InstanceType was introduced in 2.8.

Syntax

type Args = ConstructorParameters<typeof Person>;
type PersonObject = InstanceType<typeof Person>;

Examples

ConstructorParameters and InstanceType: working example

Run this standalone example after compiling with TypeScript 5.4+ in strict mode, targeting ES2022. Expected result: The program prints Ada: 3; changing the constructor’s parameter types updates the factory’s checked arguments.

class Person {
  constructor(public name: string, public visits: number) {}
  describe() { return `${this.name}: ${this.visits}`; }
}
type Args = ConstructorParameters<typeof Person>;
type PersonObject = InstanceType<typeof Person>;
function createPerson(...args: Args): PersonObject {
  return new Person(...args);
}
console.log(createPerson("Ada", 3).describe());
  1. Expected result: The program prints Ada: 3; changing the constructor’s parameter types updates the factory’s checked arguments.
  2. typeof Person refers to the constructor value’s type.
  3. The rest parameter receives the extracted tuple, preserving order and parameter types.

Best practices

  • Prefer deriving factory signatures over duplicating argument lists.
  • Be aware that overload-based extraction follows the final applicable signature rules.

Runtime & versions

Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

TypeScript Handbook

Specifications & further reading