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)