codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

TypeScript

20 / 60 topics
15Interfaces in TypeScript16Type Aliases17Union Types18Intersection Types19Literal Types20Type Assertions
Tutorials/TypeScript/Type Assertions
🔷TypeScript

Type Assertions

Updated 2026-05-15
10 min read

Type Assertions

Introduction

In TypeScript, type assertions allow you to tell the compiler that you know more about a value than it can infer. This is useful when you have information about a variable's type that TypeScript doesn't have access to at compile time. Type assertions are a way to cast a value to a specific type, telling TypeScript to treat the value as if it were of that type.

Type assertions come in two forms: angle-bracket syntax and as syntax. Both serve the same purpose but can be used in different contexts depending on your preference and the language mode you're using (JavaScript or JSX).

Concept

Type assertions are not a feature for changing the runtime behavior of your code; they are purely for compile-time checks. This means that TypeScript will trust your assertion and won't emit any type errors related to it, but if the assertion is incorrect, you may encounter runtime errors.

Angle-Bracket Syntax

The angle-bracket syntax is more common in JavaScript files:

TypeScript
1let someValue: any = "this is a string";
2let strLength: number = (<string>someValue).length;

as Syntax

The as syntax is preferred in TypeScript files and works well with JSX:

TypeScript
1let someValue: any = "this is a string";
2let strLength: number = (someValue as string).length;

Both of these examples tell TypeScript that someValue should be treated as a string, allowing you to access the .length property.

Examples

Basic Type Assertion

Let's start with a simple example where we assert a value to a specific type:

TypeScript
1let someValue: any = "this is a string";
2let strLength: number = (someValue as string).length;
3console.log(strLength); // Output: 16

Type Assertion with Union Types

Type assertions can be particularly useful when dealing with union types. For example, if you have a variable that could be either a string or a number, but you know it's definitely a string in a certain context:

TypeScript
1function getLength(input: string | number): number {
2return (input as string).length;
3}
4
5console.log(getLength("hello")); // Output: 5
6// console.log(getLength(123)); // Error: Property 'length' does not exist on type 'number'.

Type Assertion with Interfaces

You can also assert values to interfaces or classes:

TypeScript
1interface Person {
2name: string;
3age: number;
4}
5
6let person = {} as Person;
7person.name = "John";
8person.age = 30;
9
10console.log(person); // Output: { name: 'John', age: 30 }

Type Assertion with Generics

Type assertions can be used in conjunction with generics to provide more specific type information:

TypeScript
1function getProperty<T, K extends keyof T>(obj: T, key: K) {
2return obj[key];
3}
4
5let person = { name: "John", age: 30 };
6let nameLength = (getProperty(person, "name") as string).length;
7console.log(nameLength); // Output: 4

What's Next?

Now that you have a good understanding of type assertions, the next step is to explore more advanced TypeScript features such as classes. Classes in TypeScript provide a way to define blueprints for creating objects and encapsulate data and behavior.

Stay tuned for our upcoming tutorial on "Classes in TypeScript" where we will dive deeper into how to use classes effectively in your TypeScript projects!


PreviousLiteral TypesNext Classes in TypeScript

Recommended Gear

Literal TypesClasses in TypeScript