Type Casting in TypeScript

Learn via video courses
Topics Covered

Overview

Type casting is also referred to as type conversion it is a process of transforming one data type into another data type in typescript.Type casting is necessary when we need to override a type. With the help of using type casting, the compiler changes one data type to another data type depending on what we want to do in the program. There are two methods in which we can do typecasting in typescript, we will learn about both of them in detail in this article.

Introduction

As we already know that type casting allows us to convert a variable from one type to another type, but it is not supported in javascript as it is dynamic while in typescript we can declare the type of every variable explicitly. There is no concept of type casting in javascript mainly because it has dynamic types. There are mainly two ways to typecast a variable in typescript:

  1. Using the as keyword.
  2. Using the <> operator. Let's see about the typescript cast types in detail

Type Casting Types Supported by Typescript

Two types of support type casting:

a. Built-in-types: These are the basic and the inbuilt andpes such as numbers, string boolean, etc.

b. The additional types: These types are user-defined that is we have to declare them according to use they are also called user-defined types such as enums, arrays, tuples, etc.

Now let's see the syntax of typecasting in typescript:

Syntax

As we know that there are two types to typecast a variable so basically there will be two syntaxes. Let's learn about them:

Hence we have learned about both the syntaxes of how to typecast a variable and implement it in our program. Now let's read about the importance and usage of using type casting in typescript

Why is Type Casting in Typescript Needed?

Type casting in typescript(typescript cast) is used to convert a variable's data type to another data type according to the conditions of the program. For example, converting a number data type to a string data type. Type casting is used to ensure whether the variables are correctly processed by a function or not. Now let's read about the types of typecasting in detail with examples:

Type Casting: Using the Keyword

One of the ways in typescript cast to typecast a variable is using the as keyword,which directly changes the type of the variable. Now let's see an example below of how to typecast a variable using the as keyword.

Output

Now let's see another example:

Output

The following program gives an error because the number does not have a length. Type casting does not change the type of the data within the variable, for example, the following code will not work as expected because the variable var1 is holding a number.

Type casting: Using the <> Operator

Another way to typecast a variable (typescript cast) is by using the <> operator, which also directly changes the type of the variable. Syntax

Let's see a few examples using the <> operator of how to implement it in our program:

Output

In the above code, we have typecast the variable into the string and then calculated its length of it. Let's see another example:

Output

Typescript: Type Conversion Example

a. Type Casting a string type to a number using ‘as’

We can use them as a keyword to typescript cast the string type to a number. Let's see this with the help of an example:

Output

Here in this code var1 and len are string and number types the length of the string inside the variable var1 is stored in the variable length. The length of the string taken will be displayed when the code is executed. Run the above code in your editor for a better explanation.

b.Type Casting a string type to a number using ’<>’

We can use the <> operator to cast the string type to a number. Let's see this with the help of an example:

Output

Here in this code var1 and len are string and number types the length of the string inside the variable var1 is stored in the variable length. The length of the string taken will be displayed when the code is executed. Run the above code in your editor for a better explanation.

c. Type Casting a number type to a string using ’as’

We can typecast a number type to a string type using the as keyword. Let's see this with the help of an example:

Output

In the above code n and var1 are initialized as number and string types. The number type is attempting to cast to the string in this case. Run the above code in your editor for a better explanation.

d. Type Casting a number type to a string using ’unknown’:

We can use the unknown keyword to cast a number type to a string. Let's see this with the help of an example:

Output

In the above code, the cast type of the variable is first set to unknown and then typecasted in the above code. Run the above code in your editor for a better explanation.

Conclusion

  • Type casting allows us to convert a variable from one type to another.
  • There are two ways in which we can typecast a variable.
  • The concept of typecasting is not present in javascript and is present in typescript.
  • Type casting is used to override the type of a variable.
  • Type casting is mainly used to enrich the user experience by detecting and correcting mistakes before the developer begins to deploy the code.