Q45. What is the difference between type-level and runtime validation?
Type-level checking reasons about source code and declared types before execution. Runtime validation inspects actual values while the program runs. External inputs need runtime checks because their shape cannot be guaranteed by a TypeScript annotation or interface.
Use static types for internal contracts and runtime schemas or guards at boundaries such as HTTP requests and persisted JSON.
Note
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
TypeScript concept
// Illustrative TypeScript contract
type Identifier = string;
const id: Identifier = "item-1";💡 The example demonstrates the compile-time contract discussed in this question.
Key Takeaways
- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
Knowledge Check
Which approach checks the actual shape of received JSON?