Syntax

TypeScript Type Assertion Syntax

In TypeScript, type assertion: tells the compiler to treat a value as a type — const el = document.getElementById("x") as HTMLInputElement;

Quick Answer

Syntax
const el = document.getElementById("x") as HTMLInputElement;
TypeScriptSyntax

Type Assertion

Code example
const el = document.getElementById("x") as HTMLInputElement;

Tells the compiler to treat a value as a type.

Learn More

How to type assertion in TypeScript

  1. Open your TypeScript file or editor.
  2. Copy the example, then replace its sample names and values for your code.

TypeScript Type Assertion Syntax

Use const el = document.getElementById("x") as HTMLInputElement;.

Related Syntax References

Frequently asked questions

What is the TypeScript syntax for type assertion?

Use this pattern: const el = document.getElementById("x") as HTMLInputElement;

Can I copy this TypeScript code example?

Yes. Use Copy code and adapt the example to your program.

Is this TypeScript syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify