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
- Where does TypeScript stop protecting you, and what do you do at those points?hardAlso on typescript and type-erasure4 min
- This form marks invalid fields with red text and a red border. What has to change?mediumAlso on validation4 min
- A churn model scores 0.94 AUC offline and barely beats the baseline in production. One of its strongest features is populated by a process that runs after the customer has already churned. Explain what happened and how you would fix it.mediumAlso on validation5 min
- Why would you bind configuration with @ConfigurationProperties rather than @Value, and how do you make a bad value fail at startup instead of at the first request?mediumAlso on validation4 min