TypeScript · Narrowing and unions

Type assertion

Overrides inference. Prefer narrowing or validation; assertions can hide real bugs.

Syntax
value as Shape

Example

Example
const raw = JSON.parse('{"kind":"circle","r":2}') as { kind: string };
console.log(raw.kind);

More in this group

Assertion function Discriminated union Exhaustive switch in operator instanceof guard Non-null assertion Type predicate typeof guard

Learn this in the course

Reference pages are the quick answer. The course explains the why, shows the full example and ends with a challenge.

Open the course