Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Part 5.3Fetching data safely

Calling response.json() gives data from outside your program. A TypeScript annotation cannot prove that a server actually returned that shape.

JSON is unknown

TypeScript
const response = await fetch("/api/user");
const data: unknown = await response.json();

Validate at the boundary

Use a type guard to test the data before your application uses it. This keeps uncertainty at the network boundary instead of pretending a cast has made untrusted data safe.

Feedback 👍
Comments are sent via email to me.