EN
TypeScript - variable type casting
14
points
In this article, we would like to show how to cast variable from one type to another using TypeScript.
Quick solution:
const object: any = 'This is my text...';
const text1: string = object as string; // approach 1
const text2: string = <string> object; // approach 2
Casting using as
keyword
This approach is recommended when we use JSX syntax (React, Preact, SolidJS, etc.).
const object: any = 'This is my text...';
const text: string = object as string;
Casting using <>
brackets
const object: any = 'This is my text...';
const text: string = <string> object;