← Back to TypeScript Course | Chapter 5: Type Aliases and Union Types | Lesson 8 of 9

instanceof Guard

The instanceof operator checks whether an object was created from a particular class or constructor. TypeScript uses successful instanceof checks to narrow the value to the corresponding class type.

Basic instanceof Checks

Use value instanceof ClassName to determine whether an object is an instance of a specific class at runtime, and TypeScript narrows the value's type inside the branch where the check succeeds. This works because instanceof is a real JavaScript runtime check backed by the prototype chain, not something TypeScript invents just for typing.

Example: Basic instanceof Checks

typescript
class Car { drive() { console.log("Driving"); } }
class Bike { pedal() { console.log("Pedaling"); } }
function move(vehicle: Car | Bike) {
  if (vehicle instanceof Car) {
    vehicle.drive();
  } else {
    vehicle.pedal();
  }
}
move(new Car());

instanceof with Date

Built-in classes such as Date can also be checked with instanceof, which is useful when a value's origin is ambiguous, such as data returned from a third-party library that might hand back either a Date object or a plain string.

Example: instanceof with Date

typescript
function printDate(value: string | Date) {
  if (value instanceof Date) {
    console.log(value.getFullYear());
  } else {
    console.log(value);
  }
}
printDate(new Date(2024, 0, 1));

instanceof with Error

Error objects are commonly checked with instanceof when handling caught values in a catch block, since catch clauses type their parameter as unknown and instanceof lets you safely narrow it to Error (or a custom subclass) before reading .message.

Example: instanceof with Error

typescript
try {
  throw new Error("Something failed");
} catch (e) {
  if (e instanceof Error) {
    console.log(e.message);
  }
}

instanceof with Custom Classes

Custom classes are often used as discriminators when objects have behavior as well as data, letting instanceof distinguish between class instances the same way it distinguishes built-ins, which is especially handy in an OOP-style codebase with multiple related classes.

Example: instanceof with Custom Classes

typescript
class Admin { manage() { console.log("Managing"); } }
class Guest { browse() { console.log("Browsing"); } }
function handle(user: Admin | Guest) {
  if (user instanceof Admin) {
    user.manage();
  } else {
    user.browse();
  }
}
handle(new Guest());

instanceof with Multiple Classes

When a union contains several class types, multiple instanceof checks can narrow the value step by step, similar to a chain of if/else if branches, until only one possible class remains at each point in the code.

Example: instanceof with Multiple Classes

typescript
class Circle {}
class Square {}
class Triangle {}
function name(shape: Circle | Square | Triangle): string {
  if (shape instanceof Circle) return "Circle";
  if (shape instanceof Square) return "Square";
  return "Triangle";
}
console.log(name(new Square()));
🔒

Chapter Quiz — Complete all 9 topics to unlock

0/9 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.