
- TypeScript - Home
- TypeScript - Roadmap
- TypeScript - Overview
- TypeScript - Environment Setup
- TypeScript - Basic Syntax
- TypeScript vs. JavaScript
- TypeScript - Features
- TypeScript - Variables
- TypeScript - let & const
- TypeScript - Operators
- TypeScript - Types
- TypeScript - Type Annotations
- TypeScript - Type Inference
- TypeScript - Numbers
- TypeScript - Strings
- TypeScript - Boolean
- TypeScript - Arrays
- TypeScript - Tuples
- TypeScript - Enums
- TypeScript - Any
- TypeScript - Never
- TypeScript - Union
- TypeScript - Literal Types
- TypeScript - Symbols
- TypeScript - null vs. undefined
- TypeScript - Type Aliases
- TypeScript Control Flow
- TypeScript - Decision Making
- TypeScript - If Statement
- TypeScript - If Else Statement
- TypeScript - Nested If Statements
- TypeScript - Switch Statement
- TypeScript - Loops
- TypeScript - For Loop
- TypeScript - While Loop
- TypeScript - Do While Loop
- TypeScript Functions
- TypeScript - Functions
- TypeScript - Function Types
- TypeScript - Optional Parameters
- TypeScript - Default Parameters
- TypeScript - Anonymous Functions
- TypeScript - Function Constructor
- TypeScript - Rest Parameter
- TypeScript - Parameter Destructuring
- TypeScript - Arrow Functions
- TypeScript Interfaces
- TypeScript - Interfaces
- TypeScript - Extending Interfaces
- TypeScript Classes and Objects
- TypeScript - Classes
- TypeScript - Objects
- TypeScript - Access Modifiers
- TypeScript - Readonly Properties
- TypeScript - Inheritance
- TypeScript - Static Methods and Properties
- TypeScript - Abstract Classes
- TypeScript - Accessors
- TypeScript - Duck-Typing
- TypeScript Advanced Types
- TypeScript - Intersection Types
- TypeScript - Type Guards
- TypeScript - Type Assertions
- TypeScript Type Manipulation
- TypeScript - Creating Types from Types
- TypeScript - Keyof Type Operator
- TypeScript - Typeof Type Operator
- TypeScript - Indexed Access Types
- TypeScript - Conditional Types
- TypeScript - Mapped Types
- TypeScript - Template Literal Types
- TypeScript Generics
- TypeScript - Generics
- TypeScript - Generic Constraints
- TypeScript - Generic Interfaces
- TypeScript - Generic Classes
- TypeScript Miscellaneous
- TypeScript - Triple-Slash Directives
- TypeScript - Namespaces
- TypeScript - Modules
- TypeScript - Ambients
- TypeScript - Decorators
- TypeScript - Type Compatibility
- TypeScript - Date Object
- TypeScript - Iterators and Generators
- TypeScript - Mixins
- TypeScript - Utility Types
- TypeScript - Boxing and Unboxing
- TypeScript - tsconfig.json
- From JavaScript To TypeScript
- TypeScript Useful Resources
- TypeScript - Quick Guide
- TypeScript - Cheatsheet
- TypeScript - Useful Resources
- TypeScript - Discussion
TypeScript - Type Assertions
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 doesnt 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' }