Q32: What are Mapped Types and how do you iterate over keys?
Mapped Types create new types by iterating over keys using the in operator combined with keyof.
Custom Mapped Type
type CustomOptions<T> = {
[K in keyof T]: boolean;
};
interface Flags {
darkMode: string;
notifications: number;
}
type BooleanFlags = CustomOptions<Flags>;
// Result: { darkMode: boolean; notifications: boolean; }💡 Iterates through every key K in type T and assigns boolean as the property type.
Key Takeaways
- Mapped types iterate across key collections to generate derived structures.