Suggest an editImprove this articleRefine the answer for “What does InstanceType<T> do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`InstanceType<T>`** is a built-in TypeScript utility type that **returns the instance type** created by the constructor `T`. **Key point:** it extracts the type of the object that `new T()` creates.Shown above the full answer for quick recall.Answer (EN)Image## 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: - `T` is the **constructor type** (for example, `typeof MyClass`); - `infer R` extracts 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; // string ``` --- ### 2. 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>; // Base ``` --- ### 5. With built-in constructors ```javascript type A = InstanceType<typeof String>; // string type B = InstanceType<typeof Number>; // number type C = InstanceType<typeof Date>; // Date ``` --- ## What 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: Product ``` --- ### 2. 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 | OrderService ``` --- ## Difference 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>; // Person ``` --- ## Summary | 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. |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.