Mapped Types
ELI5 — The Vibe Check
Mapped types let you create new types by transforming every property of an existing type. It's like a for-loop but at the type level — 'for each property in this type, make it optional/readonly/whatever.'
Real Talk
A TypeScript feature that creates new types by iterating over the keys of an existing type using the 'in' keyword. Mapped types can modify property modifiers (readonly, optional), remap keys, and transform value types, serving as the foundation for built-in utility types like Partial and Readonly.
Show Me The Code
type Nullable<T> = {
[K in keyof T]: T[K] | null;
};
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
interface User { name: string; age: number; }
type UserGetters = Getters<User>;
// { getName: () => string; getAge: () => number; }
When You'll Hear This
"Mapped types with key remapping let you generate getter/setter types automatically." / "All of TypeScript's utility types like Partial and Required are just mapped types under the hood."
Related Terms
Conditional Types
Conditional types are if-else statements for TypeScript's type system. 'If T extends string, give me a number, otherwise give me a boolean.
Template Literal Type
Template literal types let TypeScript understand string patterns at the type level.
TypeScript
TypeScript is JavaScript with a strict parent watching over it.
Utility Types
Utility types are TypeScript's built-in type transformers — like Instagram filters but for your interfaces.