Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Utility Types

TypeScript InstanceType<T> utility type

Reference for TypeScript InstanceType<T>: get the type of the objects a constructor creates, for classes, abstract classes, factories and mixins.

InstanceType<T> takes a constructor type and returns the type of the objects it builds. For a class Foo, the type typeof Foo is the constructor (the class value) and Foo itself is the instance type, so InstanceType<typeof Foo> is just Foo.

That sounds redundant for plain classes, and it is. The utility earns its place in generic code, where a function receives a class as an argument and has to describe what new on it will produce.

TypeScript
class Cat {
  meow() {}
}

type C = InstanceType<typeof Cat>;
// Cat

Try it

TypeScript A registry of constructors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Constructor type versus instance

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
type InstanceType<T extends abstract new (...args: any) => any> =
  T extends abstract new (...args: any) => infer R ? R : any;
Name Description
T A constructor type such as typeof SomeClass, or any new (...) => X signature.

Result: the type R produced by new T(...).

Edge cases: InstanceType<any> is any; InstanceType<never> is never. A plain function type is rejected because it has no new signature.

Usage notes

  • Class value versus type. Writing a class name in a type position means the instance type; typeof ClassName means the constructor. Mixing them up is the most common error.
  • Abstract classes are accepted, so you can name the instance type of an abstract base without ever constructing it.
  • Generic classes. For class Box<T>, InstanceType<typeof Box> gives Box<unknown>, because the type parameter cannot be inferred from the class value.
  • Constructor signatures. An object type with a new signature works too, which is how many libraries describe their classes.
  • With mixins. A mixin function returns a constructor type; InstanceType of it describes the combined object.
  • Argument list: ConstructorParameters<T> is the companion for the parameters.
  • Not the same as ReturnType. A class is not callable, so ReturnType<typeof Foo> is an error.
  • Available since TypeScript 2.8.