Type Guards in TypeScript

Learn via video courses
Topics Covered

Overview

Type guards in typescript are a technique which is used to get information about the type of the object within a conditional block. As we know that typescript is statically typed and we get to know about most of the issues during compile time only ,so here type guards come into play as they help detect more runtime failures that the TypeScript framework can not handle. The Type guard methods perform in the runtime and return a boolean value.

Introduction

As we already know type guards help us to narrow down the type of object within a conditional block.Type guards are mainly used for narrowing the type and are quite similar to feature detection and allow us to detect the correct methods, prototypes, and properties of a value. Type guards are regular functions that return a boolean value and it takes a type and tells TypeScript whether it can be narrowed down to something more specific or not.There are various methods to use a type of guard.

  • The typeof keyword
  • The instanceof keyword
  • The in keyword
  • Equality narrowing type guard

Type Guards

As we already what are type guards in typescript,so basically type guards are one of the most really important features of TypeScript. It allows us to check types in your code to assure that our data-flow relies on the correct data types. Type guards are a special kind of expression that is used to change the type of a variable .

Now lets read about each of the major ways to use a type guard in detail:

The typeof Keyword

By the word typeof we know that it is used to determine the type of the keyword. The typeof is used to determine the following types:

  • String
  • bigint
  • symbol
  • undefined
  • function
  • number If we use the typeof keyword for something which is outside this list then the typeof type guard simply returns an object. The typeof type guard can be written in two ways lets see how we can write it in typescript:

Now lets see an example of how to implement it in our program:

Here in this code empid is of union type string | number if the type of var1 is a string then the employee is printed else if it is of number then empid is printed .The typeof type guard helps us to extract the type from var1.Run the above code in your editor/ compiler for a better and clear explanation.

The Instanceof Keyword

The Instanceof Keyword is an advanced version of the typeof type guard.The instanceof type is used to check whether the given object has properties similar to the specific class or constructor function according to which the operator returns a boolean value. Let's see the basic syntax of instanceof type guard in typescript:

Now lets see an example of the instanceof type:

Above is an example of the instanceof type guard where the instanceof type guard compares both classes to effectively determine the type.Run the above code in your editor for a better explanation. Now lets read about the in type guard in typescript:

The in Type Guard

The in type does a check if a property is present on an object and can be used as a type guard.The in type guard is helpful for functional validations and avoiding runtime issues. Lets see the syntax of how we can the in type guard in typescript:

Now lets see an example of how to implement the in type guard in our code:

Now lets read about the Equality Narrowing Type Guard in typescript:

Equality Narrowing Type Guard

Equality narrowing type guards return a value based on the type of the variable ,it returns true if the variables are same else it returns false. It is mainly used to check the value for an expression. Lets see an example of how to implement it in typescript:

If variable num1 is equal to variable num2, then both have the same type. In this case, Typescript narrows it down to a number.Run the above code in your editor for a better and clear explanation. Lets see another example of an equality narrowing type guard:

In the above code if narrowing takes place the answer will be a boolean else the answer will be either a boolean or a number.

User Defined Type Guards

User defined type Guards mean that the user can declare a particular type Guard or you can help TypeScript to infer a type when we want to use, like for example, a function. If we want to declare your own type guards with custom logic to help the TypeScript compiler determine the type we will need to declare a function that serves as a type guard using any logic we will like.User Defined Type Guard is a function that returns a type predicate in place of a return type.

Run the above code in your editor which will help you understand the code more properly.

Conclusion

  • Type guards are very much useful for narrowing types.
  • Type guards help us to ensure type safety and write code which is easier to reason about and easier to maintain and is more readable.
  • Type guards helps us improve the overall code flow.
  • Using type guards in typescript makes the code more readable and less error prone.
  • Most of the time our problem will e solved using only instance or type but if required we w can use the customer type guard also.
  • The narrowing down of types with the help of Type Guards is extremely useful when we get a bunch of types and need to run the code which might differ for different types.