Why Runtime Validation Matters
You'll learn: Why static type checks (like JSDoc or @ts-check) can't protect you from real-world data, and why runtime validation is essential for robust applications.
The Static vs. Runtime Gap
Static type checking—whether through TypeScript, JSDoc, or @ts-check—happens before your code runs. It analyzes your source code and catches type mismatches based on what it can see at compile time. But here's the critical limitation: static checks disappear when your code actually executes.
Think of it like a recipe review. A chef can review your recipe (static check) and confirm you listed "2 cups of flour." But when you actually bake the cake (runtime), someone might hand you salt instead. Static checks can't protect against what happens when your code meets the real world.
Where Static Types Fall Short
Your JavaScript runs in an unpredictable environment:
- API responses might change format without warning
- User input can be anything (malicious or accidental)
- Environment variables might be missing or malformed
- Third-party data doesn't come with type guarantees
- JSON parsing produces
any-typed data that bypasses all checks
Static types assume trust. They say "this function expects a number," but they can't stop someone from calling it with untrusted data from fetch() or JSON.parse(). Once your code runs, TypeScript and JSDoc annotations are gone—they don't exist in the executed JavaScript.
Why Validation Libraries Are Essential
Runtime validation libraries like Zod, Yup, or Joi actually check data while your code runs. They verify shapes, types, and constraints at the boundaries where external data enters your system—exactly where static types stop working.
Key Takeaway: Static types document intent and catch developer mistakes; runtime validation protects your application from the unpredictable outside world. You need both for truly type-safe JavaScript.