”;
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.
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.
How to Perform Type Assertions?
Type assertion is a Typescript technique that tells the compiler about the type of variable. 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
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);
The output will be:
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
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);
The output will be:
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
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);
The output will be:
{ name: ''Tutorialspoint'', value: ''typescript'' }
”;