31. What is the purpose of `type guards` in TypeScript, and how do they enhance type safety? Provide examples.

What is the purpose of type guards in TypeScript, and how do they enhance type safety?

Type guards are a way to narrow down the type of a variable within a conditional block. They allow TypeScript to understand what type a variable is, helping to ensure that operations on that variable are safe and appropriate for its type. Type guards are particularly useful in scenarios involving union types, where a variable may be one of several different types.

How Type Guards Work

Type guards are usually implemented using conditional checks such as typeof, instanceof, or user-defined type predicates.

  1. Using typeof:

    function printId(id: number | string) { if (typeof id === 'string') { console.log('Your ID is ' + id.toUpperCase()); } else { console.log('Your ID is ' + id.toFixed(2)); } }

    In this example, the typeof operator is used to check if id is a string or a number, allowing for safe method calls on each type.

  2. Using instanceof:

    class Dog { bark() { console.log('Woof!'); } } class Cat { meow() { console.log('Meow!'); } } function makeSound(animal: Dog | Cat) { if (animal instanceof Dog) { animal.bark(); } else { animal.meow(); } }

    Here, instanceof is used to determine if animal is an instance of Dog or Cat, allowing the correct method to be called.

  3. User-Defined Type Guards:

    interface Fish { swim(): void; } interface Bird { fly(): void; } function isFish(pet: Fish | Bird): pet is Fish { return (pet as Fish).swim !== undefined; } function move(pet: Fish | Bird) { if (isFish(pet)) { pet.swim(); } else { pet.fly(); } }

    In this example, a custom type guard function isFish is created to determine if a pet is a Fish. The pet is Fish part is a type predicate, which informs TypeScript about the type inside the if block.

Benefits of Type Guards

  • Enhanced Type Safety: By narrowing types within conditional blocks, you reduce the risk of runtime errors and incorrect assumptions about the type.
  • Improved Code Readability: Type guards make it clear what type a variable is expected to be at different points in the code.
  • Better Tooling Support: With accurate type information, IDEs can provide better code completion and error checking.

Overall, type guards are a powerful feature in TypeScript that ensure your code works safely and as expected, especially when dealing with complex types.

Struggling to find common date to meet with your friends? Try our new tool commondate.xyz
devFlipCards 2025

Do you accept cookies?

Cookies are small amounts of data saved locally on you device, which helps our website - it saves your settings like theme or language. It helps in adjusting ads and in traffic analysis. By using this site, you consent cookies usage.

Struggling to find common date to meet with your friends? Try our new tool
commondate.xyz