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.
const input = document.getElementById("name") as HTMLInputElement;
Try it
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.
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.
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
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(orany) to override. - Asserting to a subtype is allowed (
PettoDog); 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 numberis still a string. - Prefer narrowing (
typeof,in,instanceof, type predicates) over assertions; the compiler then checks your logic. - Prefer
satisfieswhen 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 returnsnullwhen missing. - Non-null assertion
x!is a separate shorthand; see non-null assertion. - ESLint rules such as
no-unnecessary-type-assertioncatch useless ones. - Use sparingly: every assertion is a place where the type system trusts you.