TypeScript "as" Keyword

Learn via video courses
Topics Covered

Overview

The as keyword is a type assertion in typescript. Type assertion is used such that we can set the data type of the variable on our own and tells the compiler not to infer it on its own. 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. The as keyword is just a way to type assertion in typescript.

Introduction to TypeScript "as" Keyword

The as keyword is used for making the type assertions in typescript. It considers the object as another type than the type the compiler infers the object to be. Now let's see the syntax of implementing as a keyword in typescript:

By using this syntax we can assert types in typescript using the as keyword. Now let us see an example using the syntax which we have read:

Output

In the above code, we have created a variable named l and stored a string value in it, and then calculated the length of the string using the as keyword to convert the string l from unknown to string type. Run the above code in your editor for a better and clear explanation.

What's the TypeScript "as" Keyword Used for?

Typescript as a keyword is used for type assertion it is used to override the data type of the variable. Suppose we have a variable of type number, by using type assertion we can change the data type of that variable from number to string. The as keyword is just one of the methods of doing type assertion in typescript. Let us see this with the help of an example:

Output

In the above code since we were confident that the data type of the variable a is a string but it has been declared as any so using the keyword we have type asserted the variable to type string. Thus using the as keyword we can override the type of the variable, but we must be confident enough or else our program might get an error.

How Does the “as” Keyword Work in TypeScript?

As we know that typescript is a typed language and everything has its type in typescript, but the compiler is sometimes incorrect and may infer the wrong type in some cases, in that cases typescript has the property to assert types. The as keyword is just a method to assert types in typescript. The as keyword is used to do both type assertion as well as const type assertion. Now let's see an example how as keyword works in typescript:

In the above code we have created a variable named id and stored a numeric value in it and then using the as keyword converted the variable id from unknown to number type This is the main use of the type assertion as it helps us to forcefully convert the data type of the variable using the as a keyword. Run the above code in your editor for a better and clear explanation.

Example of Type Assertion in Typescript

Though we have studied many examples of type assertion in typescript now let us another good example to clear all our doubts:

Output

Explanation In this example, we have created four variables and assigned them with nos, strings, and boolean values, but the data type is unknown since we are sure about the data type of the variables so using the as keyword we have type asserted the variables. Run the above code in your editor for a better and clear explanation.

When to Use the "as" Keyword?

As we know the as keyword is just a method of type assertion in typescript so we should use it only when we want to assert another data type to it and we are enough confident about it as any mistake may cause an error in the code. The as keyword can be used in those cases where the type of the object is known though unknown to the compiler to perform a type assertion in typescript using the as keyword to associate the required type to the object or variable.

Use Cases

Use the "as" Keyword to Cast Types in TypeScript

The keyword in typescript can be used cast a type that can be a little more specific or a little less specific version of the expected type we want. Let us see an example:

Here in this example, we are forcing the compiler to relate the user object with the type student for good auto-completion and suggestions which results in a better development experience one could have. Run the above code in your editor for a better and clear explanation.

Use the as Keyword in Type Predicates in TypeScript

The as keyword is also used in type predicates. Type predicates are type guards which are used as type guards on untyped objects or objects with weak types. Now let us see an example:

Explanation In the above code the stud1 is used as a type predicate. This is how the type predicate is used as type guards and in other ways. Run the code in your editor for a better and clear explanation.

Conclusion

  • Type Assertion should be used only if you are confident about the topic or else it will lead to errors in the program.
  • By using the as a keyword in typescript, we can forcefully change the normal type checking and forced the compiler to treat our user as an object.
  • The as keyword is just one of the methods which are used to type assertion in typescript.
  • Type assertion is similar to typecasting but it doesn't reconstruct code