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:

Community

roadmap.sh is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store 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

ThewNewStack

The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.