# Mapped Types

> Looping over keys to generate new object types.

Subject: TypeScript | Lesson 30 of 54 | 5 min read
Interactive lesson: https://codewithanimation.in/typescript/mapped-types
Previous: [The infer Keyword](https://codewithanimation.in/typescript/infer-keyword.md) | Next: [Key Remapping (as)](https://codewithanimation.in/typescript/key-remapping.md)

## The Type-Level Map

In JavaScript, you use `.map()` to loop over an array and transform its values.

In TypeScript, a Mapped Type loops over a union of string keys (usually from `keyof`) to generate a brand new object type.

## Don't Duplicate Types

If you have a `User` type, and you need a new type for the "Edit Profile" form where every field is optional, you shouldn't create a `PartialUser` type by copy-pasting and adding `?` everywhere.

Instead, you use a mapped type to dynamically loop over the original `User` keys and append the `?` modifier automatically.

## Syntax

```typescript
type User = { id: number; name: string };

// Loop over every key in T. Add the `?` modifier.
// The value type is extracted using T[K] (Indexed Access).
type MyPartial<T> = {
  [K in keyof T]?: T[K];
};

type PartialUser = MyPartial<User>;
// Result:
// {
//   id?: number;
//   name?: string;
// }
```

## Common mistake: Removing Modifiers

What if a type already has `readonly` properties, and you want to map it to create a mutable version?

You can add a minus sign (`-`) in front of the modifier in a mapped type to *strip* it away during the loop: `[-readonly K in keyof T]-?: T[K]` removes both readonly and optional modifiers!

## Tip: Built-in Utilities

TypeScript provides built-in mapped types out of the box: `Partial<T>`, `Required<T>`, `Readonly<T>`, and `Record<K, T>`.

## Try it: Try it

Toggle the mapping modifiers and watch how the `[K in keyof T]` loop iterates over the original `User` interface, applying the modifications to every single property.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/mapped-types

## Check yourself

### Question 1

What does the mapped type `type Booleans<T> = { [K in keyof T]: boolean }` do?

- A. It filters the original type, keeping only properties that were already booleans.
- B. It creates a new object type with the same keys as T, but sets every single value type to 'boolean'.
- C. It throws an error because T[K] is missing.

Correct answer: B

A mapped type loops over the original keys, but you dictate what the new value is. In this case, every value is overwritten to 'boolean'.

- Why not A: Mapped types don't filter properties by default; they transform them.
- Why not C: You aren't required to use T[K]. You can map every key to a hardcoded type.

## Remember this

- Mapped types use the syntax `[K in keyof T]: Type`.
- They act like a loop, iterating over each key to construct a new object type.
- You can add modifiers during the loop by adding `readonly` or `?`.
- You can strip modifiers during the loop by prefixing them with a minus sign (`-readonly` or `-?`).
