Visit complete TypeScript roadmap

← Back to Topics List

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:

Roadmaps Guides Videos About YouTube

roadmap.sh by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© roadmap.sh · FAQs · Terms · Privacy