Type Assertion
A type assertion tells the compiler to treat a value as a different type than it inferred. It changes nothing about the value at runtime; it only changes what the type checker believes.
Detailed Explanation
Sometimes you know more than the compiler does. document.getElementById("email") is typed HTMLElement | null, but you wrote the markup and you know it is an <input>. An assertion is how you say so:
const field = document.getElementById("email") as HTMLInputElement;
Read as as "trust me". That is the whole feature. There is no conversion, no check, no validation. The emitted JavaScript is document.getElementById("email") with the assertion deleted. If the element is actually a <div>, nothing throws at the assertion — you get a confusing failure later, when you read .value and find undefined.
People call this "casting", which is misleading. In C# or Java, a cast can inspect the object and throw. In TypeScript the operation is erased, so there is nothing left to throw. The honest name is the one the spec uses: an assertion.
Two syntaxes
The original form was angle brackets: <HTMLInputElement>el. It still works in .ts files. It does not work in .tsx files, because <HTMLInputElement> is indistinguishable from an opening JSX tag, and the parser resolves the ambiguity in favour of JSX. Since almost every codebase has some .tsx in it, as is the form to learn and the form every style guide now mandates.
Assertions are not unrestricted
The compiler allows an assertion only between types that overlap — one has to be assignable to the other. Widening HTMLElement to HTMLInputElement is fine, because the input type is a subtype. Asserting string to number is not:
const n = "42" as number;
// Error: Conversion of type 'string' to type 'number' may be a mistake because
// neither type sufficiently overlaps with the other. If this was intentional,
// convert the expression to 'unknown' first.
The error text names the escape hatch. "42" as unknown as number compiles, because everything is assignable to unknown and unknown is assertable to anything. This double assertion is the loudest thing you can write in TypeScript. Treat it as a marker that you are stepping outside the type system deliberately, and leave a comment explaining why. It is legitimate when mocking in tests or working around a wrong third-party type; it is a bug waiting to happen everywhere else.
Assertion versus satisfies
Both appear after an expression, and they do opposite things. An assertion overrides the checker: you claim a type and it stops arguing. satisfies asks the checker: it verifies the expression matches a type and then keeps the narrow inferred type. If you want confirmation that a config object is valid, use satisfies. Use an assertion only when you have information the compiler cannot have.
The cost of an assertion is that you have taken responsibility for something the compiler was doing for free. Assertions do not rot loudly. When the underlying type changes, the assertion keeps compiling and keeps being wrong.
Code Examples
1. The legitimate case: you know the concrete DOM type, the lib types cannot.
// 1. The legitimate case: you know the concrete DOM type, the lib types cannot.
const field = document.getElementById("email") as HTMLInputElement;
field.value = "ada@example.com"; // .value exists on HTMLInputElement, not HTMLElement
// The safer alternative when you are not certain — a runtime check instead:
const el = document.getElementById("email");
if (el instanceof HTMLInputElement) {
el.value = "ada@example.com"; // narrowed by a real check that runs
}2. Assertions only run between related types; 'unknown' is the documented bypass.
// 2. Assertions only run between related types; 'unknown' is the documented bypass.
type Session = { userId: string; expiresAt: number };
const raw: unknown = JSON.parse('{"userId":"u_1","expiresAt":0}');
const session = raw as Session; // allowed: unknown asserts to anything, checks nothing
const cents = "1999" as number;
// Error: Conversion of type 'string' to type 'number' may be a mistake...
const forced = "1999" as unknown as number; // compiles; still the string "1999" at runtime
console.log(forced + 1); // "19991" — string concatenation, not arithmetic3. Assertion lies, satisfies checks.
// 3. Assertion lies, satisfies checks.
type Theme = Record<string, `#${string}`>;
const asserted = { primary: "blue" } as Theme; // no complaint, and "blue" is not a hex colour
const checked = { primary: "blue" } satisfies Theme;
// Error: Type '"blue"' is not assignable to type '`#${string}`'.
// Angle-bracket form: equivalent in .ts, a parse error in .tsx.
const legacy = <HTMLInputElement>document.getElementById("email");Common Gotchas
An assertion is not a runtime conversion
`value as number` does not parse, coerce, or check anything. Use `Number(value)` to convert, and a type guard or validation library to check.
`as unknown as T` silences the one warning that was protecting you
the "neither type sufficiently overlaps" error usually means the assertion is wrong. Reach for the double assertion rarely, and comment why the compiler is mistaken.
Assertions decay silently during refactors
add a required field to `Session` and `raw as Session` still compiles, even though the object no longer has it. Nothing re-verifies an assertion once written.
`as const` is a different operator that shares the keyword
it does not override a type, it asks for the narrowest literal type and marks the value readonly. It is safe in ways `as T` is not.