Skip to main content
D
TypeScript
TypeScript Type Assertions

TypeScript Type Assertions

Type Assertions force TypeScript to accept a specific type.

Read Time
5 min read
Difficulty
Beginner
Last Updated
Jun 15, 2026
Version
v1.0.0

Introduction

Sometimes developers know more about a specific value than the TypeScript compiler does, especially when reading HTML elements from the DOM. A Type Assertion tells the compiler, "Trust me, I know exactly what type this is."

Example

Example
1/* this forces a type assertion */
2const myCanvas = document.getElementById("main_canvas") as HTMLCanvasElement;
3
4// Now TypeScript knows myCanvas has canvas-specific methods

Key Points

  • Use the `as` keyword to assert a type.
  • It overrides TypeScript's internal guessing engine.
  • It does not actually change or format the data itself.
  • Only use it when you are absolutely certain of the data type.

Up Next

Continue your journey with the next topic.

Go to TS Functions