Visit complete TypeScript roadmap
TypeScript Topic

Type Assertions

Type Assertions

Type assertions in TypeScript are a way to tell the compiler to treat a value as a specific type, regardless of its inferred type.

There are two syntaxes for type assertions in TypeScript:

  • The “angle-bracket” syntax: <T>value
  • The “as” syntax: value as T

For example:

let num = 42;

// using angle-bracket syntax
let str = <string>num;

// using as syntax
let str2 = num as string;

In both examples, num is a number, but the type assertions tell the compiler to treat the value as a string.

Learn more from the following links:

More Topics

Explore related content

View All Topics
Loved by 100K+ Developers

Start Your Learning
Journey Today

Join thousands of developers who are leveling up their skills with structured roadmaps and expert guidance

No credit card required
Always free
Track your progress