Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Type Operators

TypeScript as type assertions

Reference for the as type assertion in TypeScript: narrowing and widening, the sufficient-overlap rule, double assertions through unknown, and safer alternatives.

value as Type is a type assertion. It tells the compiler to treat value as Type from that point on. It does not convert anything and does not check anything at runtime: the emitted JavaScript contains only value. If your claim is wrong, the program keeps running with a lie in its types.

Assertions are for moments when you know more than the compiler can prove, such as a DOM lookup, parsed JSON that you have validated elsewhere, or a value created in a way that types cannot follow.

TypeScript
const input = document.getElementById("name") as HTMLInputElement;

Try it

TypeScript Narrowing with as

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript When as refuses

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Double assertion and safer options

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
expression as Type
<Type>expression        // angle-bracket form, not allowed in .tsx files
expression as const     // see "as const"
Part Description
expression The value whose static type you want to override.
Type The claimed type. It must be sufficiently overlapping with the expression's type: one must be assignable to the other (comparable).

Errors and edge cases:

  • Unrelated types are rejected: "Conversion of type 'number' to type 'string' may be a mistake". Go through unknown (or any) to override.
  • Asserting to a subtype is allowed (Pet to Dog); asserting to a supertype is always allowed.
  • Assertions on object literals do not trigger excess property checks the way annotations do.

Usage notes

  • No runtime effect. There is no cast, no check, no conversion. "5" as unknown as number is still a string.
  • Prefer narrowing (typeof, in, instanceof, type predicates) over assertions; the compiler then checks your logic.
  • Prefer satisfies when you only want validation.
  • Prefer annotation when you declare a variable: const x: T = ... is checked, an assertion is not.
  • DOM: querySelector<HTMLInputElement>("#id") uses a generic instead of an assertion and still returns null when missing.
  • Non-null assertion x! is a separate shorthand; see non-null assertion.
  • ESLint rules such as no-unnecessary-type-assertion catch useless ones.
  • Use sparingly: every assertion is a place where the type system trusts you.