Blog / Typescript

How to Use Type Guards and Type Assertions in TypeScript

How to Use Type Guards and Type Assertions in TypeScript

In this post find out about how to Use Type Guards and Type Assertions in TypeScript

Will MaygerWill Mayger
May 27, 2023
Article

TypeScript provides powerful features like type guards and type assertions that allow you to work with complex types and handle dynamic type checks in a more robust and reliable manner. Type guards help you narrow down the type of a value within a conditional block, while type assertions allow you to assert the type of a value when you have more information than the compiler. In this article, we’ll explore how to use type guards and type assertions effectively in TypeScript.

Understanding Type Guards

Type guards in TypeScript allow you to narrow down the type of a value within a conditional block based on a runtime check. They provide a way to make more precise type distinctions and enable the compiler to infer the correct type in subsequent code blocks. There are several ways to implement type guards in TypeScript:

  1. typeof Type Guard:

    function logLength(value: string | number) {
      if (typeof value === "string") {
        console.log(value.length); // Access string-specific property
      } else {
        console.log(value.toFixed(2)); // Access number-specific method
      }
    }

    In this example, the typeof type guard is used to check if value is a string. If the check evaluates to true, the code block within the if statement is executed with the type narrowed down to string.

  2. instanceof Type Guard:

    class Dog {
      bark() {
        console.log("Woof!");
      }
    }
    
    class Cat {
      meow() {
        console.log("Meow!");
      }
    }
    
    function makeSound(animal: Dog | Cat) {
      if (animal instanceof Dog) {
        animal.bark(); // Access Dog-specific method
      } else {
        animal.meow(); // Access Cat-specific method
      }
    }

    In this example, the instanceof type guard is used to determine if animal is an instance of Dog. If it is, the code block within the if statement is executed with the type narrowed down to Dog.

  3. User-Defined Type Guard:

    interface Fish {
      swim(): void;
    }
    
    function isFish(animal: any): animal is Fish {
      return typeof animal.swim === "function";
    }
    
    function swim(animal: Fish | string) {
      if (isFish(animal)) {
        animal.swim(); // Access Fish-specific method
      } else {
        console.log("Not a fish!");
      }
    }

    In this example, a user-defined type guard isFish is created to determine if animal has a swim method. If the type guard returns true, the code block within the if statement is executed with the type narrowed down to Fish.

Type guards allow you to handle different types within conditional blocks and perform type-specific operations. They enable the compiler to infer more precise types, ensuring type safety and avoiding runtime errors.

Using Type Assertions

Type assertions in TypeScript allow you to assert the type of a value when you have more information than the compiler. They are similar to type casts in other languages and provide a way to override the default type inference or to work with types that the compiler cannot determine automatically. Type assertions are expressed using the angle bracket (<>) syntax or the as keyword. Here are some examples:

  1. Angle Bracket Syntax:

    let value: any = "Hello";
    let length: number = (value as string).length;

    In this example, the type assertion (value as string) is

used to tell the compiler that value should be treated as a string. The resulting length variable is of type number and is assigned the length of the string.

  1. as Keyword:

    let value: any = "Hello";
    let length: number = (<string>value).length;

    This example achieves the same result as the previous one, but with the as keyword instead of the angle bracket syntax.

Type assertions should be used with caution, as they essentially tell the compiler to trust the developer’s knowledge of the type. Improper use of type assertions can lead to runtime errors if the type assertion doesn’t match the actual type of the value.

Combining Type Guards and Type Assertions

Type guards and type assertions can be used together to achieve more precise type checking and to leverage the strengths of both features. By using type guards to narrow down the type of a value and type assertions to inform the compiler about the specific type, you can work with complex types more confidently. Here’s an example:

interface Car {
  brand: string;
  startEngine(): void;
}

interface Bicycle {
  type: string;
  pedal(): void;
}

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

function drive(vehicle: Car | Bicycle) {
  if (isCar(vehicle)) {
    vehicle.startEngine(); // Access Car-specific method
  } else {
    vehicle.pedal(); // Access Bicycle-specific method
  }
}

In this example, the isCar type guard is used to determine if vehicle is a Car. If it is, the code block within the if statement is executed with the type narrowed down to Car. Type assertions (vehicle as Car) and (vehicle as Bicycle) are used to ensure the compiler understands the specific types being accessed in each code block.

Conclusion

TypeScript provides type guards and type assertions to handle dynamic type checks and to work with complex types more effectively. Type guards enable you to narrow down the type of a value within a conditional block, allowing the compiler to infer more precise types and ensure type safety. Type assertions, on the other hand, provide a way to override the default type inference or to work with types that the compiler cannot determine automatically. By combining type guards and type assertions, you can achieve more precise type checking and confidently handle complex type scenarios in your TypeScript code. Understanding and utilizing type guards and type assertions will improve the reliability and maintainability of your TypeScript applications.

There we have how to Use Type Guards and Type Assertions in TypeScript, if you want more like this be sure to check out some of my other posts!

Foxi - Budget Planner & Tracker

Foxi

Budget Planner & Tracker

More money in your pocket by the end of the month.

Free to use and no account needed.

Get started now.

Get the app

Some graphics used on this post were made using icons from flaticon.

Latest Posts

Learn React, JavaScript and TypeScript

Learn React, JavaScript and TypeScript

Join the platform that top tier companies are using.
Master the skills you need to succeed as a software engineer and take your career to the next level with Pluralsight.

Start here

Become an expert in ReactJS, TypeScript, and JavaScript.

Here you will find my personal recomendations to you, for full disclosure I earn a small commission from some of these links, but I only recommend what I trust and personally use.

Good things are coming, don't miss out!

Good things are coming, don't miss out!

Follow me on Twitter to stay up to date and learn frontend, React, JavaScript, and TypeScript tips and tricks!

Are you a novice, intermediate or expert react engineer?

Find out here by taking my fun, interactive, quick quiz which takes approximately 1 - 3 minutes. How well will you will do?

Foxi - Budget Planner & Tracker

Foxi

Budget Planner & Tracker

More money in your pocket by the end of the month.

Free to use and no account needed.

Get started now.

Get the app