Open this lesson on a wider screen to play it. The narrated transcript and animated code need room to sit side by side.
← All lessons// Plain TypeScript: a thrown error has no type. `catch` hands you `unknown`,
// and nothing in the signature warned you a failure was even possible.
function parseConfig(json: string): { port: number } {
return JSON.parse(json) as { port: number }; // may throw — invisible in the type
}
try {
const config = parseConfig("{ not json");
console.log(config.port);
} catch (err) {
// err is `unknown`; the compiler can't tell you what went wrong.
console.log("parse failed");
}