Skip to content

Mapped Types

Spicy — senior dev territoryGeneral Dev

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.