What if your types could magically update themselves whenever your classes change?
Why InstanceType type in Typescript? - Purpose & Use Cases
Imagine you have a class in TypeScript and you want to create a type that exactly matches the type of an object created by that class. Doing this manually means you have to copy all properties and methods by hand or guess the shape of the instance.
This manual approach is slow and error-prone because if the class changes, you must update the type everywhere. It's easy to miss updates, causing bugs and type mismatches that are hard to find.
The InstanceType type automatically extracts the instance type from a class or constructor function. This means your types always match the actual objects created, without extra work or risk of mistakes.
type UserInstance = { name: string; age: number; greet: () => void; }type UserInstance = InstanceType<typeof User>
You can keep your types perfectly in sync with your classes, making your code safer and easier to maintain.
When working with a class like User that has many properties and methods, InstanceType lets you create variables or function parameters typed exactly like a User object without rewriting the type.
Manually writing instance types is slow and error-prone.
InstanceType extracts the instance type automatically from a class.
This keeps your types accurate and your code easier to maintain.
Practice
InstanceType do?Solution
Step 1: Understand the purpose of
InstanceTypeInstanceTypeis a utility type that extracts the type of an object created by a class constructor.Step 2: Compare options with the definition
Only Extracts the type of an instance created by a class correctly describes this behavior. Options B, C, and D describe unrelated actions.Final Answer:
Extracts the type of an instance created by a class -> Option BQuick Check:
InstanceType extracts instance type = A [OK]
- Confusing InstanceType with creating instances
- Thinking it converts types to strings
- Assuming it defines primitive types
InstanceType with a class named Car?Solution
Step 1: Recall the syntax of InstanceType
InstanceTypeexpects a constructor type, so we passtypeof Carto get the class constructor type.Step 2: Analyze each option
type CarInstance = InstanceType; usestypeof Car, which is correct. type CarInstance = InstanceType<Car>; passes the class itself, which is a value, not a type. Options A, B, and C use invalid syntax.Final Answer:
type CarInstance = InstanceType; -> Option AQuick Check:
Use typeof with class name inside InstanceType for correct syntax = D [OK]
- Passing class name directly without typeof
- Using parentheses after class name
- Trying to instantiate inside InstanceType
myDog?
class Dog {
name: string;
constructor(name: string) {
this.name = name;
}
}
let myDog: InstanceType;
myDog = new Dog('Buddy');Solution
Step 1: Understand what InstanceType means
It extracts the instance type created by the Dog class constructor, which is the class Dog itself.Step 2: Match the type of myDog
myDog is typed as an instance of Dog, so its type is Dog (the class instance type). Dog matches this exactly.Final Answer:
Dog -> Option DQuick Check:
InstanceType = Dog instance type = A [OK]
- Confusing instance type with constructor type
- Choosing typeof Class instead of instance
- Picking object shape instead of class type
InstanceType:
class Cat {
age: number;
constructor(age: number) {
this.age = age;
}
}
type CatInstance = InstanceType<Cat>;
const kitty: CatInstance = new Cat(3);Solution
Step 1: Check the usage of InstanceType
InstanceTyperequires a constructor type, so the class name must be prefixed withtypeof.Step 2: Identify the error in the code
The code usesInstanceType<Cat>instead ofInstanceType, causing a type error.Final Answer:
Missing typeof before Cat in InstanceType -> Option AQuick Check:
InstanceType needs typeof ClassName = C [OK]
- Omitting typeof before class name
- Thinking InstanceType works on instances
- Assuming InstanceType works without constructor type
Circle and Square. You want to create a function that accepts an instance of either class using InstanceType. Which of the following is the correct way to type the parameter shape?
class Circle {
radius: number;
constructor(radius: number) {
this.radius = radius;
}
}
class Square {
side: number;
constructor(side: number) {
this.side = side;
}
}
function drawShape(shape: ???) {
// drawing logic
}Solution
Step 1: Understand the goal
The function should accept instances of Circle or Square, so the parameter type must represent instances, not classes or constructors.Step 2: Analyze each option
InstanceType | InstanceType correctly usesInstanceTypeandInstanceTypecombined with a union to represent instances of both classes. Circle | Square uses class names directly, which are types of instances but does not use InstanceType. typeof Circle | typeof Square uses constructor types, which is incorrect for instances. InstanceType tries to use InstanceType on a union of classes, which is invalid.Final Answer:
InstanceType | InstanceType -> Option CQuick Check:
Use InstanceType with typeof for each class, combine with | for union [OK]
- Using class names without InstanceType
- Using typeof Class instead of InstanceType
- Trying InstanceType on union of classes directly
Ready to prove it?
Pass the test and this pattern moves unknown
