TypeScript 7 reference · Utility Types
TypeScript InstanceType<Type> Utility Type
Get the type of the object a class constructor creates, from the constructor's type.
TypeScript 7 Since 2.8
Syntax
InstanceType<Type>
Parameters
| Parameter | Description |
|---|---|
Type |
A constructor type (typeof SomeClass). |
Return value
The instance type.
Examples
class Logger {
constructor(public prefix: string) {}
log(msg: string) {
console.log(`${this.prefix} ${msg}`);
}
}
type LoggerInstance = InstanceType<typeof Logger>;
const l: LoggerInstance = new Logger("[app]");
l.log("started");
Output
[app] started
function make<C extends new (...args: any[]) => any>(Cls: C, ...args: ConstructorParameters<C>): InstanceType<C> {
return new Cls(...args);
}
class Point {
constructor(public x: number, public y: number) {}
}
const p = make(Point, 3, 4);
console.log(p.x + p.y, p instanceof Point);
Output
7 true
Version history
| Version | Change |
|---|---|
| TypeScript 2.8 | Added |
Related
ReturnType<Type>· Get the return type of a function type, so other code can reuse it without repeating it.- TypeScript Utility Types · tutorial