What is the InstanceType utility type?
Why Interviewers Ask This
This question targets practical, hands-on experience with TypeScript. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.
Answer
InstanceType<T> extracts the instance type of a constructor function or class. It takes a class constructor type and returns the type of the instances that constructor creates. Implementation: type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any. Usage: class User { name: string; constructor(name: string) { this.name = name; } } type UserInstance = InstanceType<typeof User>; — UserInstance is the same as the User type (the instance type). Most useful when working with class references passed as parameters or stored in variables: function createInstance<T extends new (...args: any) => any>(ctor: T): InstanceType<T> { return new ctor(); } — this factory function returns the correct instance type regardless of which class is passed. Related: ConstructorParameters<T> extracts the constructor's parameter types.
Pro Tip
This topic has TypeScript-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.
Previous
What is module augmentation in TypeScript?
Next
What is the Awaited utility type in TypeScript?