as

Performs a type assertion telling the compiler to treat a value as a given type.

Since TS 1.6 Spec ↗

Syntax

expression as TargetType

Examples

const el = document.getElementById('app') as HTMLInputElement;
el.value = 'hello';
const data = JSON.parse(raw) as { id: number };
const wide = 'on' as const; // const assertion

Notes

Assertions are compile-time only and perform no runtime check or conversion. TypeScript only allows assertions between types with some overlap; use `as unknown as T` to force an unrelated cast. Prefer type guards over assertions where possible. `as const` narrows to literal readonly types.

See also