Skip to content

OutputNot attempted

Type assertions do nothing at runtime

type User = { name: string; age: number };
const data: unknown = JSON.parse('{"name":"Ann"}');
const user = data as User;
console.log(user.age.toFixed(0));

Spaces and quote style don’t matter · write a thrown error as TypeError

Ctrl Enter
Show answer & explanation

The expected output comes from actually running the snippet; opening this shows it next to the explanation.

Types are erased: as User is a promise to the compiler, not a check. age is missing at runtime, so .toFixed throws TypeError. Use a type guard or a schema validator (zod) for data from the network.