InstanceType<T>
Extracts the instance type of a constructor function type T, giving you the type of objects created by the constructor.
Definition
type InstanceType<T extends abstract new (...args: any) => any> =
T extends abstract new (...args: any) => infer R ? R : any;Examples
class HttpClient {
baseUrl: string;
constructor(baseUrl: string) {
this.baseUrl = baseUrl;
}
get(path: string) {
return fetch(this.baseUrl + path);
}
}
type Client = InstanceType<typeof HttpClient>;
// { baseUrl: string; get(path: string): Promise<Response> }
const client: Client = new HttpClient("https://api.example.com");function createSingleton<T extends new (...args: any[]) => any>(
Cls: T,
...args: ConstructorParameters<T>
): InstanceType<T> {
let instance: InstanceType<T> | null = null;
return new Proxy({} as InstanceType<T>, {
get(_, prop) {
if (!instance) instance = new Cls(...args);
return (instance as any)[prop];
},
});
}abstract class Plugin {
abstract name: string;
abstract init(): void;
}
type PluginConstructor = new () => Plugin;
type PluginInstance = InstanceType<PluginConstructor>;
const registry: Map<string, PluginInstance> = new Map();
function register(Cls: PluginConstructor) {
const instance = new Cls();
registry.set(instance.name, instance);
}Common Use Cases
- 1Typing the result of generic factory functions
- 2Working with class references passed as parameters
- 3Plugin systems that instantiate classes dynamically
- 4Dependency injection containers
- 5Typing class instances when only the constructor is available
Understanding InstanceType<T>
InstanceType<T> extracts the instance type of a constructor function type. While a class name in TypeScript already represents the instance type, InstanceType is essential when you have a reference to the constructor itself (typeof ClassName) and need to know what type of object it creates.
This distinction matters in generic programming. When a function accepts a class as a parameter (type T extends new (...args: any) => any), T represents the constructor, not the instance. InstanceType<T> converts back from the constructor type to the instance type, letting you correctly type the objects created with new.
Factory patterns are the primary use case. A generic create function that accepts any class and its arguments needs InstanceType to express its return type. Without it, you'd have to use any or resort to manual type assertions.
Plugin systems and dependency injection containers also rely heavily on InstanceType. These systems typically maintain registries of class constructors and need to express the type of instances they create. InstanceType bridges this gap cleanly.
In testing, InstanceType helps type mock instances. If you have a mock factory that creates test doubles for a given class, InstanceType<typeof RealClass> ensures the mock matches the shape of real instances.
InstanceType works with abstract classes too, thanks to its abstract new constraint. This means you can use it with abstract base classes to express the type of any concrete subclass instance, which is valuable in frameworks that work with class hierarchies.
Related Types
ConstructorParameters<T>Extracts the parameter types of a constructor function type T as a tuple type.
ReturnType<T>Extracts the return type of a function type T, enabling you to reuse a function's output type without manual duplication.
Parameters<T>Extracts the parameter types of a function type T as a tuple, enabling you to reuse a function's argument types elsewhere.
typeof (Type Operator)The typeof type operator extracts the TypeScript type from a value-level variable or expression, bridging the value and type worlds.
More Object & Function Types
Explore TypeScript Types
Browse our complete reference of 30 TypeScript utility types with definitions, examples, and explanations.