🚀Why Type Guards Are Necessary in TypeScript: An Introduction to Type Guards and Their Use Cases

altaj1 - Nov 8 - - Dev Community

In TypeScript, one of the main goals is to make your code safer and reduce errors by enforcing type checks. But sometimes, you may still run into scenarios where you aren’t sure of a variable’s type, or you may have a variable that could be of multiple types. This is where type guards come in.

Type guards help TypeScript understand what type a variable has at a particular point in your code, allowing you to work more safely with your data and avoid common runtime errors. In this post, we’ll break down why type guards are essential and introduce different types of type guards with real-life examples.

Why Are Type Guards Necessary?

TypeScript’s type system provides safety, but it’s limited when dealing with union types—variables that could be one of multiple types (e.g., string | number). Without type guards, TypeScript cannot always figure out what type a variable is. For example, you might want to use a method specific to strings, but if TypeScript isn’t sure your variable is a string, it will throw an error.
Type guards help solve this problem by narrowing down the type, allowing TypeScript to "guard" the code that depends on a specific type. This approach minimizes errors and makes your code cleaner and easier to read.

Types of Type Guards in TypeScript

There are several ways to implement type guards in TypeScript. Let’s go through the most common ones:

1. typeof Type Guard
The typeof operator is a quick and easy way to check for primitive types like string, number, or boolean. This is useful when you need to work with data that could be multiple primitive types.

Example:

function formatValue(value: string | number) {
  if (typeof value === "string") {
    return value.toUpperCase(); // TypeScript knows `value` is a string here
  } else {
    return value.toFixed(2); // TypeScript knows `value` is a number here
  }
}
Enter fullscreen mode Exit fullscreen mode

Use Case: Use the typeof guard when you need to handle different types of primitive values in a single function. For instance, formatting a value differently based on whether it's a string or a number.

2. instanceof Type Guard
The instanceof operator checks if an object is an instance of a specific class. This is helpful when working with class-based objects.

Example:

class Dog {
  bark() {
    console.log("Woof!");
  }
}

class Cat {
  meow() {
    console.log("Meow!");
  }
}

function makeNoise(animal: Dog | Cat) {
  if (animal instanceof Dog) {
    animal.bark(); // TypeScript knows `animal` is a Dog here
  } else {
    animal.meow(); // TypeScript knows `animal` is a Cat here
  }
}
Enter fullscreen mode Exit fullscreen mode

**Use Case: **Use instanceof when you’re dealing with multiple classes, and you want to perform specific actions based on the class type. For example, handling different actions for dogs and cats based on their respective classes.

3. Custom Type Guards with in Operator
The in operator is used to check if a property exists in an object. This is useful with objects that have specific properties unique to their types.

Example:

type Bird = { fly: () => void };
type Fish = { swim: () => void };

function move(animal: Bird | Fish) {
  if ("fly" in animal) {
    animal.fly(); // TypeScript understands `animal` is a Bird here
  } else {
    animal.swim(); // TypeScript understands `animal` is a Fish here
  }
}
Enter fullscreen mode Exit fullscreen mode

Use Case: Use the in operator when working with object types that have specific properties. This is especially handy when types share some properties but have unique properties that set them apart.

4. User-Defined Type Guards (Custom Functions)
User-defined type guards allow you to create custom type-checking functions that return a boolean. These are helpful when dealing with complex types or advanced cases where the built-in type guards don’t cover your needs.
Example:

type Car = { make: string; drive: () => void };
type Bike = { brand: string; ride: () => void };

function isCar(vehicle: Car | Bike): vehicle is Car {
  return (vehicle as Car).drive !== undefined;
}

function start(vehicle: Car | Bike) {
  if (isCar(vehicle)) {
    vehicle.drive(); // TypeScript knows `vehicle` is a Car here
  } else {
    vehicle.ride(); // TypeScript knows `vehicle` is a Bike here
  }
}
Enter fullscreen mode Exit fullscreen mode

Use Case: Use custom type guards when you need a more specific check than what typeof, instanceof, or in can offer. For instance, determining if a vehicle can "drive" or "ride" in a transport app.

Summary

Type guards are a powerful feature in TypeScript that make it easier to write safe, error-free code by narrowing down types at runtime. To recap:
typeof is best for primitive types.
instanceof works well with classes.
in operator is ideal for objects with unique properties.
Custom type guards allow you to create specific type-checking functions.
By using type guards, you can confidently handle variables with multiple types and build more reliable applications. Whether you’re working on a simple project or a large-scale application, type guards will help you write code that’s cleaner, safer, and easier to debug.

.