In TypeScript, type assertion serves as a tool that informs the compiler regarding the type of a variable. When TypeScript identifies an assignment as invalid, we have the ability to bypass the type check through type assertion. By employing a type assertion, the assignment is deemed valid regardless, so it is crucial to ensure that our assertion is accurate. Failing to do so may lead to issues in the functionality of our program.
Type assertion involves explicitly indicating to the compiler that we wish to handle an entity as a different type. It enables us to interpret any type as a number, or a number as a string. This technique is frequently utilized during the transition of code from JavaScript to TypeScript.
Type assertion functions similarly to typecasting; however, it does not involve type verification or data restructuring as seen in languages such as C# and Java. Typecasting is accompanied by runtime support, while type assertion remains unaffected by runtime considerations. Instead, type assertions are exclusively a compile-time feature that offers guidance to the compiler regarding our preferences for code analysis.
Example
let empCode: any = 111;
let employeeCode = <number> code;
console.log(typeof(employeeCode)); //Output: number
In the previous illustration, we defined a variable empCode with the type any. In the subsequent line, we assign the value of this variable to another variable called employeeCode. In this case, we recognize that empCode is a number type, despite its declaration as 'any.' When assigning empCode to employeeCode, we have asserted its type as number. Consequently, the type of employeeCode is now number.
TypeScript offers two methods for Type Assertion. These are:
- Utilizing Angular Brackets <>
- Employing the as keyword
Using Angular Bracket <>
In TypeScript, we can utilize angular brackets <> to indicate Type Assertion.
Example
let empCode: any = 111;
let employeeCode = <number> code;
Using as Keyword
TypeScript offers an alternative method for demonstrating Type Assertion through the use of the " as " keyword.
Example
let empCode: any = 111;
let employeeCode = code as number;
Type Assertion with object
At times, we may encounter a scenario in which an object is defined without any properties. In such cases, the compiler will generate an error. However, this issue can be circumvented through the use of type assertion. This can be illustrated with the following example.
Example
let student = { };
student.name = "Rohit"; //Compiler Error: Property 'name' doesn?t exist on type '{}'
student.code = 123; //Compiler Error: Property 'code' doesn?t exist on type '{}'
In the preceding example, a compilation error will occur since the compiler interprets the type of student as {}—an empty object without any properties. This issue can be circumvented by employing a type assertion, as illustrated below.
interface Student {
name: string;
code: number;
}
let student = <Student> { };
student.name = "Rohit"; // Correct
student.code = 123; // Correct
In the preceding example, we established an interface named Student, which includes the attributes name and code. Subsequently, we applied type assertion on the student, representing the appropriate method for utilizing type assertion.