Why do we check API data at runtime? What is unknown for?
Answer out loud or in your head first, then compare.
Answer
TypeScript types help check our code, but a server can send something other than it promised. So I first treat data from outside as unknown and check its shape. Zod is a library in which I describe the expected fields and conditions and then check the received value against them. Only after that check do I use the data as a specific type.
unknown makes you confirm what you're working with. any lets you use a value almost freely and easily hides bugs. Writing as User doesn't check the JSON. Also, a valid object doesn't have to be allowed: a document can have correct fields and still belong to another user. Checking data and checking the right to it are separate jobs.
Why it matters: Common ground for APIs, LLM output and tool arguments.
Possible follow-ups: Will a schema confirm that the given price is real?
How did it go? Be honest — “Not yet” puts the question on your review list.
Start with
- B-P01 · What is Node.js, and how is it different from JavaScript in the browser? — JavaScript, TypeScript and Node.js
- B-P04 · What is a Promise? — JavaScript, TypeScript and Node.js
- B-P10 · What does TypeScript give you, and what doesn't it? — JavaScript, TypeScript and Node.js
Related questions
- any vs unknown — how do you handle unknown data? — TypeScript
Practise in code
- Warm-up: checking input — runs tests in your browser
Further reading
- Zod: Introduction(opens in a new tab)Accessed 22.10.2026. Schemas and checking data at runtime.
- TypeScript for JavaScript Programmers(opens in a new tab)Accessed 22.10.2026. Type basics and their role while you work on code.