TypeScript Type Assertion

Note: If you're new to TypeScript, check our Getting Started with TypeScript tutorial first.


Type assertion lets you manually tell TypeScript the type of a value. It doesn't change the value; it just tells TypeScript to treat the value as a specific type.

Here's a simple example of type assertion in TypeScript. You can read the rest of the tutorial to learn more.

Example

let value: any = "hello";
let strLength = (value as string).length;

console.log(strLength);

// Output: 5

Here, we are telling TypeScript to treat value as a string, so that we can safely access the string's length property.


Type Assertion Syntax

There are two ways to write a type assertion:

1. Using as (recommended)

let value = "hello" as string;

2. Using angle-bracket <>

let value = <string>"hello";

Type Assertion with Object

let user = {} as { name: string; age: number };

user.name = "Bob";
user.age = 30;

console.log(user);

Output

{ name: 'Bob', age: 30 }

Normally, TypeScript wouldn't allow you to assign name or age to an empty object. But, with type assertion:

let user = {} as { name: string; age: number };

you are telling TypeScript to treat user as an object that has the name and the age properties.


Why is Type Assertion Necessary?

TypeScript tries to infer the type of a variable to catch errors at compile time. However, there are times when TypeScript doesn't have enough information — for example:

  • API responses
  • DOM elements
  • Dynamic data from external sources

In such cases, TypeScript limits what we can do with the variable because it's usually typed as any.

That's where type assertion helps — it lets us tell TypeScript the exact type of the value so we can safely perform operations on it.


Frequently Asked Questions

When not to use Type assertion?

You shouldn't use type assertion when you are not sure about the actual type of a value. For example,

let value: any = "hello";

// Incorrect assertion — value is NOT a number
let num = value as number;

console.log(num * 2); // Runtime error: NaN

Forcing the wrong type can trick TypeScript into thinking everything is fine, but it may cause errors at runtime.


Type Assertion with API Data

When working with APIs, the data returned is often typed as any. TypeScript doesn't know the exact structure of the response, so in such cases, type assertion helps you define the response data.

For example,

const response = await fetch("/api/user");
const data = await response.json();

// Tell TypeScript what structure to expect
const user = data as { id: number; name: string };

console.log(user.name); // Now safely accessible

Without assertion, TypeScript won't let you access user.name directly, since it doesn't know the structure of data.


Type Assertion Vs. Type Casting

Type Assertion Type Casting
Tells TypeScript how to treat a value. Converts value from one type to another.
No effect at runtime(only at compile time). Changes the value at runtime.
Helpful with any, APIs and DOM. Used to convert between types (eg, float to int) mostly in traditional languages like C++ and Java.

Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges