DEV Community

Cover image for Why Are Type Guards Necessary? Exploring Different Types and Their Use Cases
Naym Hossen
Naym Hossen

Posted on

Why Are Type Guards Necessary? Exploring Different Types and Their Use Cases

Why Are Type Guards Necessary? Exploring Different Types and Their Use Cases

In TypeScript, type guards play an essential role in making the codebase more reliable, and developer-friendly. They help ensure type safety by allowing developers to narrow down types, which helps in runtime errors and makes code easier to understand and easy maintain.

What are Type Guards?

Type guards are expressions od functions that perform runtime checks on types, ensuring that values meet specific criteria before they are processed further. They help maintain type safety, allowing developers to handle values appropriately.

Why Are Type Guards Necessary?

In JavaScript, you often encounter values that could have multiple types, like a function that returns either a string or number. Without type guards, TypeScript wouldn’t be able to determine the exact type, leading to potential runtime errors and incorrect behavior. Type guards allow you to check the type of a value at runtime, helping TypeScript to narrow types and provide more accurate type-checking.

Common Types of Type Guards and Their Use Cases

1. typeof Type Guards

Use Case: Basic type checking for primitive types.
Example:

function printValue(value: string | number) {
  if (typeof value === "string") {
    console.log(`String value: ${value.toUpperCase()}`);
  } else {
    console.log(`Number value: ${value.toFixed(2)}`);
  }
}
Enter fullscreen mode Exit fullscreen mode

Explanation: typeof is effective with primitive types (string, number, boolean). It allows the function to handle string and number types differently.

2. Custom Type Guards (is Keyword)

Use Case: For complex logic, checking custom shapes or interfaces.

Example:

 interface Square {
    side: number;
  }
  interface Circle {
    radius: number;
  }

  function isSquare(shape: Square | Circle): shape is Square {
    return (shape as Square).side !== undefined;
  }

  function calculateArea(shape: Square | Circle) {
    if (isSquare(shape)) {
      return shape.side * shape.side;
    } else {
      return Math.PI * shape.radius * shape.radius;
    }
  }
Enter fullscreen mode Exit fullscreen mode

Explanation: Custom type guards let you define functions that check specific attributes to determine the type. Here, isSquare checks the presence of the side property.

3. in Operator Type Guards

Use Case: Checking if an object has a specific property.
Example:

 interface Car {
    drive(): void;
  }
  interface Boat {
    sail(): void;
  }

  function move(vehicle: Car | Boat) {
    if ("drive" in vehicle) {
      vehicle.drive();
    } else {
      vehicle.sail();
    }
  }

Enter fullscreen mode Exit fullscreen mode

Explanation: The in operator checks for the existence of a property, helping TypeScript understand the type and allowing access to the relevant methods.

Conclusion

Type guards are a powerful feature in TypeScript that enable precise type-checking, reduce errors, and improve code readability and maintainability. By leveraging typeof, instanceof, custom type guards, and the in operator, you can handle complex types safely and effectively in TypeScript projects.

Top comments (0)