Type assertions tell TypeScript how to treat a value. They do not change runtime behavior—they only affect types at compile time:
const input = document.getElementById('input') as HTMLInputElement;
const data = JSON.parse(text) as User;Important: Prefer real runtime checks first! If you can test with typeof, Array.isArray, or property checks, do that. Assertions are for when the type system can't see what you know.
Special assertion: as const
const colors = ['red', 'green', 'blue'] as const;
// Type: readonly ['red', 'green', 'blue']
// Not: string[]This freezes literal types, keeping exact strings and numbers instead of widening them to string or number. Perfect for small enums and tuples.
In this category, you'll:
value as Type when you have more information than the compileras const to preserve literal typesUse assertions sparingly and deliberately—they're an escape hatch when the type system needs your help.