Table of contents
1.
Introduction
2.
Using Angular(<>) Brackets
3.
Using as Keyword
4.
Type Assertion with Objects
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

TypeScript Type Assertion

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A variable is a uniquely named location used to store data in any programming language. In Typescript, the variable must be declared before being used, using the keyword let or var. The syntax for declaring variables in Typescript is a little different from other languages and looks something like this,

var [variable_name] : [type_of_variable] = [value] ;

The variable may or may not have a value and type initialized.

For example

var name: string = “Ninja”;
var name: string;
var name = “Ninja”;
var name;

All of these are correct methods of initializing a variable.

Type Assertion refers to changing the variable from one type to another explicitly. In Typescript, we refer to this as Type Assertion, whereas in some programming languages, it is also referred to as Explicit Type Conversion or Type Casting. Type Assertion can be performed using three methods,

  1. Using Angular (<>) Brackets
  2. Using “as” Keyword
  3. Type Assertion with Objects

Using Angular(<>) Brackets

This is the most basic and most commonly used type of Assertion in Typescript. The syntax is quite simple and looks like,

var num:any = 1111;
var assertedNum = <number> num;
console.log("Type of variable assertedNum: "+ typeof(assertedNum));

The type to which the variable is supposed to be converted is mentioned in angular brackets before the value. In the above example, we converted the variable “num” with data type any to “assertedNumber” with data type number

Using as Keyword

The above Type Assertion can also be implemented by replacing the angular brackets with the as keyword,

For example

var num:any = 1111;
var assertedNum = num as number;
console.log("Type of variable assertedNum: "+ typeof(assertedNum));

In this code snippet, we converted the num to a number.

var num = "Hello";
var assertedNum = num as number;
console.log("Type of variable assertedNum: "+ typeof(assertedNum));

Type Assertion restricts the conversion of “Hello” to Number as it is not a feasible conversion.

Type Assertion with Objects

It often happens that there are objects declared without any properties. For this, the compiler will throw an error, which can be avoided using Assertion. For example,

let employee = {};
employee.Name = "Ninja"; 
employee.RegNo = 123;

Let us add Assertion using objects,

interface Employee {   
    Name: string;   
    RegNo: number;   
}  
let employee = <Employee> { };   
employee.Name = "Ninja"; 
employee.RegNo = 123; 
console.log(employee.Name);
console.log(employee.RegNo)

FAQs

  1. Why is Type Assertion used?
    Type assertion allows you to set the value type and tell the compiler not to infer it. After Type Assertion, the programmer can get a better understanding of the variable types.
     
  2. When to use Type Assertion?
    It is always advised to check the variable types beforehand so that there is no need for Assertion. Still, Assertion might be necessary in a few cases where Assertion is unavoidable. For instance, it is largely used to handle DOM events. For example, the document.getElementById returns HTMLElement. It can be cast to HTMLButtonElement using Assertion.
     
  3. What are the different types of Type Assertion?
    We can implement Type Assertion in three ways, namely
  • Assertion using angular(<>) brackets
  • Assertion using “as” keyword
  • Assertion using objects

Key Takeaways

This Blog covered all the necessary points about Assertion in Typescript, discussing in-depth its functionality and different methods of the appliance of Assertion. And also its implementation in Typescript.

Don't stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass