What are mapped types, conditional types and decorators?
Practice this questionDefinition:
Mapped types build a new type by visiting the keys of another type, and conditional types choose a type based on a type relationship. Decorators are different: they run as part of JavaScript class definition and can add metadata or change behavior. Type transformations disappear during compilation, while decorator code can affect runtime behavior.
Key points:
-
Mapped types change fields:
{ [K in keyof T]?: T[K] }. -
Conditional types choose a type:
T extends string ? A : B. -
Decorators can add metadata or change runtime behavior; NestJS uses them to describe classes and routes.
-
Use mapped types to change property modifiers or value types consistently.
-
Decorator behavior depends on the decorator system and compiler/framework configuration; do not assume every decorator style works with Nest metadata.
Interview point:
- Separate compile-time tools from runtime mechanisms, even when both appear in TypeScript source.