Dmytro Morar
TypeScript

Type assertions

Type assertions tell the compiler what type a value should be. They do not change runtime values or perform checks.

Example

let value: unknown = "Hello TS";
let strLength = (value as string).length;

Syntax

const input = document.querySelector("input") as HTMLInputElement;

Notes

  • Use when you are sure of the value type.
  • Prefer type guards when possible.

On this page