Q46. How can TypeScript improve API DTO design?

Separate request and response DTOs from persistence entities when fields, serialization, or access rules differ. Use explicit mapping functions and runtime validation at the input boundary. This avoids leaking internal properties and clarifies the public contract.

A database entity may include passwordHash or a Date object, while a response DTO should expose only approved fields and serialize dates intentionally.

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

Why should a response DTO omit internal secret fields?