type assertion (TypeScript)

Writing value as Type to tell the compiler to treat a value as a type, without any check that it really is.

A wrong assertion silences the compiler and lets invalid data through unchecked.

In more detail

An assertion overrides the compiler rather than proving anything, so a wrong one moves the failure to run time. Prefer type guards, validation or satisfies. The special form as const is safe because it only makes types narrower.

Related terms

Taught in

References