Skip to content
Preptima
mediumConceptCodingScenarioMidSenior

Your endpoint takes a typed request body and it still crashed on a missing field. How is that possible when it compiled?

4 min readUpdated 2026-08-15

Answer

Short answer

TypeScript erases at compile time, so a type annotation on request data is an assertion about the future rather than a check. Anything crossing a boundary the compiler cannot see - a request body, a database row, a JSON file, an API response - has to be validated at run time, with the type derived from the validator.

Was this answer helpful?

Likely follow-ups

  • What does `as MyType` actually do to the emitted JavaScript?
  • Where else besides the request body does untyped data enter a service?
  • How do you avoid the schema and the interface drifting apart?
  • Is `unknown` better than `any` for a parsed body, and why?

Related questions