If you are looking for a way to check if object is instance of class in TypeScript, follow our tutorial to get the answer.
What is instance in TypeScript?
Like other programming languages, this concept refers to an object if it has some attributes inherited from a constructor function or a class.
Example:
class Student {
private name: string;
private age: number;
public constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
public getName(): string {
return this.name;
}
public getAge(): number {
return this.age;
}
}
const aStudent = new Student("Jane", 22);
As you can see, the object aStudent is instance of the class Student.
Check if object is instance of class in TypeScript
Method 1: Use instanceof operator
In TypeScript, you can use the instanceof operator between the class and the object you want to test. The result returns true if it is the instance of the class or returns false.
Systax:
aObject instanceof aClass
Code:
class Student {
private name: string;
private age: number;
public constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
public getName(): string {
return this.name;
}
public getAge(): number {
return this.age;
}
}
const aStudent = new Student("Jane", 22);
console.log(aStudent instanceof Student)
Result:
true
Method 2: Use constructor.name property
In TypeScript, whenever you create a class, you actually create multiple declarations at the same time. The constructor function is one of them. This is the function that is called when you new up instances of the class, and this function has a property “name,” which returns the name of the class.
Syntax:
aObject.constructor.name
Code:
class Student {
private name: string;
private age: number;
public constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
public getName(): string {
return this.name;
}
public getAge(): number {
return this.age;
}
}
const aStudent = new Student("Jane", 22);
console.log(aStudent.constructor.name);
Result:
Student
Method 3: Use Object.prototype.isPrototypeOf()
The isPrototypeOf() method checks if an object exists in another object’s prototype chain. In the expression object instanceof a class, the object prototype chain is checked against the class’s prototype, not against the class itself. The result returns a boolean value.
Syntax:
aClass.prototype.isPrototypeOf(aObject)
Code:
class Student {
private name: string;
private age: number;
public constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
public getName(): string {
return this.name;
}
public getAge(): number {
return this.age;
}
}
const aStudent = new Student("Jane", 22);
console.log(Student.prototype.isPrototypeOf(aStudent));
Result:
true
Summary
You have been through our instructions on how to check if object is instance of class in TypeScript. We hope our article will be helpful for you and if you have any questions, please leave us your comments.
Maybe you are interested:
- Define an Interface for Array of Objects in TypeScript
- Declare a function with an Object return type in TypeScript
- Check If An Object Is Empty In TypeScript

My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.
Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP