DEV Community

Supaluck Singjan
Supaluck Singjan

Posted on

Type Assertions in TypeScript

Type Assertions is the process of manually specifying data types. There are two formats.

  • as
  • <data type>

Example 1: Using the as format.

let data1:unknown;
data1="Apple";

let fruit = (data1 as string).toLowerCase();

console.log(fruit);

Enter fullscreen mode Exit fullscreen mode

Example 2: Using the <data type> format.

let data1:unknown;
data1="Apple";

let fruit = (<string>data1).toLowerCase();

console.log(fruit);

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay