Q24. Why prefer unknown for external data?
unknown allows a value of any origin but blocks unsafe operations until the code narrows it. It is a safer boundary type for parsed JSON, request bodies, local storage, and catch values than any.
Use a runtime validator or explicit guards to prove a value's shape before passing it to domain logic.
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
What should happen before unknown data is used as a User?