Type Assertion
In one line
A type assertion (value as Type or <Type>value) tells TypeScript to treat a value as a different type. It is a compile-time hint with no runtime effect.
In simple words
A type assertion overrides TypeScript's inferred type. You write value as Type to tell the compiler "trust me, this value is of type Type." It is a compile-time operation — no runtime check is performed.
Type assertions are useful when you know more than TypeScript does, e.g., parsing JSON or working with DOM elements. However, they bypass type safety — if the assertion is wrong, you get a runtime error.
Prefer type narrowing (typeof, instanceof, type guards) over assertions when possible. Use unknown instead of any for values where you need to assert, as it forces you to narrow before use.
Example
const value: unknown = JSON.parse('{"name":"Alice"}');
const obj = value as { name: string };
console.log(obj.name); // "Alice"
// DOM example
const input = document.getElementById("email") as HTMLInputElement;
console.log(input.value);Assertions tell TypeScript the type without runtime checks. Use sparingly.
Common confusions
Confused with: type annotation
The difference: Assertions override an existing type; annotations declare a type on a new variable. Assertions can be unsafe; annotations are always safe.
Related terms
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.