Languages
[Edit]
EN

TypeScript - variable casting

11 points
Created by:
Kate_C
2857

In TypeScript it is possible to make type casting in following ways.

1. as keyword example

let object : any = 'This is my text...';
let text : string = object as string;

console.log(`'${text}' has ${text.length} characters`);

Output:

'This is my text...' has 18 characters

2. <> brackets example

let object : any = 'This is my text...';
let text : string = <string>object;

console.log(`'${text}' has ${text.length} characters`);

Output:

'This is my text...' has 18 characters

 

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.