What does InstanceType<T> do?
What InstanceType<T> does
InstanceType<T> is a built-in TypeScript utility type that
returns the instance type created by the constructor T.
In other words, it extracts the type of the object created by new T().
Definition
In the TypeScript standard library it is defined as:
javascript
type InstanceType<T extends abstract new (...args: any) => any> =
T extends abstract new (...args: any) => infer R ? R : any;Breakdown:
Tis the constructor type (for example,typeof MyClass);infer Rextracts the type of the result (new T(...) -> R);- It returns precisely the instance type (
R).
Examples of InstanceType
1. A basic example with a class
javascript
class User {
name = "Alex";
age = 25;
}
type UserInstance = InstanceType<typeof User>;
// User
const u: UserInstance = new User();
u.name; // string2. With a class that has a constructor
javascript
class Point {
constructor(public x: number, public y: number) {}
}
type P = InstanceType<typeof Point>;
// Point
const p: P = new Point(10, 20);3. With a constructor type directly
javascript
type Ctor = new (id: number) => { id: number };
type Instance = InstanceType<Ctor>;
// { id: number }4. With an abstract class
javascript
abstract class Base {
abstract name: string;
}
type BaseInstance = InstanceType<typeof Base>;
// Base5. With built-in constructors
javascript
type A = InstanceType<typeof String>; // string
type B = InstanceType<typeof Number>; // number
type C = InstanceType<typeof Date>; // DateWhat happens if you pass something that is not a constructor
If you pass a type that is not a constructor, TypeScript raises an error:
javascript
type Err = InstanceType<string>;
// Type 'string' does not satisfy the constraint 'abstract new (...args: any) => any'.Practical applications
1. A universal factory
javascript
function create<T extends new (...args: any[]) => any>(
ctor: T,
...args: ConstructorParameters<T>
): InstanceType<T> {
return new ctor(...args);
}
class Product {
constructor(public name: string, public price: number) {}
}
const item = create(Product, "Shirt", 120);
// item: Product2. A dependency injection container
javascript
function use<T extends abstract new (...args: any[]) => any>(
ClassRef: T
): InstanceType<T> {
return new ClassRef();
}
class Logger { log() { console.log("Log"); } }
const logger = use(Logger); // Logger
logger.log();3. Reusing instance types
javascript
class UserService { users = ["Alex"]; }
class OrderService { orders = [1, 2, 3]; }
type Services = [typeof UserService, typeof OrderService];
type Instances = InstanceType<Services[number]>;
// UserService | OrderServiceDifference from typeof
| Construct | What it returns |
|---|---|
typeof MyClass | The constructor type |
InstanceType<typeof MyClass> | The class instance type |
Example:
javascript
class Person {}
type A = typeof Person; // new (...args: any[]) => Person
type B = InstanceType<A>; // PersonSummary
| Question | Answer |
|---|---|
What does InstanceType<T> do? | Extracts the instance type created by the constructor T. |
| What can you pass? | Only constructors (class, typeof class, new (...args) => any). |
| What does it return? | The type of the object that this constructor creates. |
| What happens if you pass something that is not a constructor? | A type error. |
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.