Languages
[Edit]
EN

TypeScript - variable type casting

14 points
Created by:
Bess
571

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;

 

Alternative titles

  1. TypeScript - object type casting
  2. TypeScript - variable types casting
  3. TypeScript - object types casting
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

TypeScript

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join