- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is Type Assertion in TypeScript?
Type assertion is a mechanism in TypeScript that informs the compiler of the variable type. We can override the type using a type assertion if TypeScript finds that the assignment is wrong. We must be certain that we are correct since the assignment is always legitimate when we employ a type assertion. If not, our program might not operate properly.
Declaratively informing the compiler that we intend to regard the item as a distinct type is known as type assertion. Using this, we can regard any as a number or a number as a string. When moving code from JavaScript to TypeScript, type assertion is frequently utilized.
Type assertion functions similarly to typecasting, but unlike C# and Java, it does not do type verification or data rearrangement. Runtime support is provided for typecasting, although type assertion does not affect runtime. Type assertions are solely a compile-time construct to give the compiler indications about how we want our code to be inspected.
In this tutorial, we will learn how to use a type assertion to tell the compiler to treat a value as a specified type.
How do Type Assertion?
Type assertion is a Typescript technique that tells the compiler the variable type. Though type assertion doesn’t recreate code, typecasting does. You can tell the compiler not to infer the type of a value by using type assertion. We utilize Type assertion to convert a variable from one type to another, such as any to a number.
To do type assertion, we can either use the “<>” operator or the “as” operator. Typecasting provides runtime support, whereas type assertion has no impact on runtime.
There are three techniques to perform Type Assertion in TypeScript, and those are:
Using “as” operator
Using “<>” operator
Using object
Using “as” Operator For Type Assertion
The "as" keyword in TypeScript offers a method for displaying Type Assertion.
Syntax
let variable_any: any = 123 let variable_number: number = variable_any as number
In the above syntax, we used the “as” keyword on any type variable for type assertion.
Example
In the below example, we used the “as” operator for type assertion. We have taken an unknown type variable whose value is “Tutorialspoint.” We used the “as” keyword to tell the compiler to treat the variable as a string and also used the string’s length property.
let variable_unknown: unknown = "Tutorialspoint"; console.log("variable_unknown value is: ", variable_unknown); let variable_number: number = (variable_unknown as string).length; console.log("Length of variable_unknown: ", variable_number);
On compiling, it will generate the following JavaScript code −
var variable_unknown = "Tutorialspoint"; console.log("variable_unknown value is: ", variable_unknown); var variable_number = variable_unknown.length; console.log("Length of variable_unknown: ", variable_number);
Output
The above code will produce the following output −
variable_unknown value is: Tutorialspoint Length of variable_unknown: 14
Using “<>” Operator for Type Assertion
The “<>” operator is another way to perform type assertion in TypeScript.
Syntax
let variable_any: any = 123 let variable_number: number = <number> variable_any
In the above syntax, we used the “<>” operator on any type variable for type assertion.
Example
In the below example, we used the “<>” operator for type assertion. We have taken an unknown type variable whose value is 12345. We used the “<>” keyword to tell the compiler to treat the variable as a number and store it in another variable of a type number. We also checked the type of the second variable to verify the type.
let my_number: unknown = 12345 console.log('my_number value is: ', my_number) let num: number = <number>my_number console.log('typeof num is: ', typeof num)
On compiling, it will generate the following JavaScript code −
var my_number = 12345; console.log('my_number value is: ', my_number); var num = my_number; console.log('typeof num is: ', typeof num);
Output
The above code will produce the following output −
my_number value is: 12345 typeof num is: number
Using “Object” For Type Assertion
Objects are another way to perform the type assertion, unlike the “as” and “<>” operators; the objects can use for multiple type assertions at once.
Syntax
interface info { name: string, value: string } let my_obj = <info> { name: 'ABC', value: 'abc'}
In the above syntax, we used the object to perform type assertion.
Example
In the below example, we used the object for type assertion. We have taken an object and set each key as a separate type, so whenever the user adds some value to them, it will automatically become the given type of value or throw an error. The my_obj uses an interface called info that contains the types, and we also console log the my_obj to see the actual result.
interface info { name: string value: string } let my_obj = <info>{} my_obj.name = 'Tutorialspoint' my_obj.value = 'typescript' console.log(my_obj)
On compiling, it will generate the following JavaScript code −
var my_obj = {}; my_obj.name = 'Tutorialspoint'; my_obj.value = 'typescript'; console.log(my_obj);
Output
The above code will produce the following output −
{ name: 'Tutorialspoint', value: 'typescript' }
- Related Articles
- What are type guards in typescript?
- Explain the symbol type in TypeScript
- What is a simple assertion in C language?
- Explain the purpose of never type in TypeScript
- What is parameter destructuring in TypeScript?
- What is the use of this keyword in TypeScript?
- Assertion : Scientist prefer metric system of units.Reason : In metric system, there are factors of 10 which are easier to remember.(1) Both assertion and reason are true. (2) Both assertion and reason are false. (3) Assertion is true, reason is false. (4) Assertion is false, reason is true.
- What are Intersection Types in typescript?
- The G Assertion in Perl
- What is type deduction in C++?
- What is type inference in C++?
- What is Type casting in C#?
- What is Type conversion in C#?
- What is Type safe in C#?
- What is type conversion in java?
