Type Assertion in TypeScript

Learn via video courses
Topics Covered

Overview

Type assertion tells the compiler about the type of the variable.Type assertion is similar to typecasting but it does not reconstruct the code or does any special checks. Type assertion is used to tell the compiler that we want to treat any as a number, or string.We must be sure while using type assertion as it disables type checking so the program may not run correctly if we are not confident enough.

Introduction

Type assertion is a technique that informs the compiler about the type of a variable. It disables type checking . Now lets us see why we should use type assertion:

We know type assertion is used when the declared variables are not defined and are set to any or unknown. In this example, empid is of type any because nothing has been mentioned during the time of initialization. Here we can use type assertion and set the variable to its original type. There are two ways of doing type assertion in typescript:

  1. Using Angular Brackets <>
  2. Using the keyword

Using Angular Brackets <>

In TypeScript, we can use angular "bracket <>" to show Type Assertion. Now let's see the syntax first:

Syntax:

Now let's see an example:

Output

In the above code first, we have declared a variable name and set it to any and have declared a name, since we know that this is a string so while assigning a name to companyname, we have asserted that name is of type string in this case, and we are sure about it. Now, the type of company name is a string. Run the above code in your editor for a better and clear explanation.

Let's see another example:

Output

In this example we have created a variable named name and set it to any then using type assertion we have asserted the type and changed the type of name as string and set the type of namelength as number and printed the length of the string.

Now let's read about the next type Using Angular Brackets

Type Assertion Using the Keyword

TypeScript provides another way to show Type Assertion by using the "as" keyword.Now let's see the syntax:

Syntax:

Now let's see some examples using this type:

Output

In the above code first, we have declared a variable name and set it to any and have declared a name , since we know that this is a string so while assigning a name to companyname, we have asserted using the as a keyword that the name is of type string in this case, and we are sure about it. Now, the type of companyname is string.Run the above code in your editor for a better and clear explanation.

In this example, we have created a variable named name and set it to any then using them as a keyword in type assertion then we have asserted the type and changed the type of name as string and set the type of namelength as number and printed the length of the string.

Type Assertion of Objects

We may sometimes have a situation in which an object is defined without any properties. The compiler will throw an error as a result of this. However this issue can be prevented by utilizing type assertion.

In the above example, we will get a compilation error, because the compiler assumes that the type of employee is {} without properties. We can avoid this situation by using a type assertion, which can be shown below.

In the above example, we have created an interface employee with the property's name and code. Then, we used type assertion on the employee, which is the correct way to use type assertion. Run the above code in your editor for a better and clear explanation.

Advantages of Using Type Assertion in Typescript:

  • In TypeScript, type assertion is a mechanism that tells the compiler about the type of a variable.
  • We can use type assertion to override the issue even when the assignment is invalid because type assertion disables type checking.
  • Type assertions are purely a compile-time construct and provide hints to the compiler on how we want our code to be analyzed.

Disadvantages of Type Assertion in Typescript:

  • By type assertion we are forcefully telling the compiler that whatever type we are assigning is right and this may lead to errors in the program as we may be wrong sometimes.
  • Type assertion does not do any special checking. That is why it is safer to declare the type of the variable explicitly.

Type Annotation vs Type Assertion

Type annotations are used to tell the compiler that a variable through its lifetime can only have a specified type. If we try to assign a different type to the then it will throw an error. Type assertion are used to cast variables to a specific type. It tells the compiler that whatever it is telling us is the right assertion no need to guess it. Now let's see an example of both types:

Type annotations

Output

Now let's see an example of type assertion:

Type assertion:

Output:

Type assertion will only succeed only if we assign to a new variable or the asserted type is a subtype of the variable type that we are assigning to. Run the above code in your editor for a better and clear explanation.

The difference between type annotation and type assertion is that type assertion weakens the type-checking feature of Typescript. While using type annotation we can make a code resilient to runtime errors, but type assertions don’t enforce that.

Conclusion

  • We should be confident enough before using type assertion as it may lead to errors in the program.
  • Type assertion disables type checking.
  • Even if a variable is invalid, by using type assertion we can override this problem.
  • We can use type assertion to specify a value’s type and tell the compiler not to deduce it.
  • We should use Type Annotations over Type Assertions as this enforces type safety and this will cause compile-time errors as if the declared type is missing any properties or is wrongly typed.