How To Check If Object Is Instance Of Class In TypeScript

Check if Object is instance of Class in TypeScript

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:

Leave a Reply

Your email address will not be published. Required fields are marked *