Skip to content

Mapped Types in TypeScript

Transform existing types by mapping over their keys with custom modifiers and filters.

What you'll learn

  • How mapped types iterate over keys with [K in keyof T]
  • Adding and removing readonly and optional modifiers with + and -
  • Filtering keys with conditional types inside mapped types
  • How built-in utility types are implemented as mapped types

Concept

The Mapping Syntax

A mapped type transforms an existing type by iterating over its keys. The syntax is { [K in keyof T]: NewType }, which creates a new type with the same keys as T but with each property's type replaced by NewType. For example, type Stringify<T> = { [K in keyof T]: string } turns every property of T into a string. The K in keyof T part is the mapping — it loops over each key of T.

Modifiers: + and -

You can add or remove the readonly and ? (optional) modifiers in a mapped type. Adding is the default: { [K in keyof T]?: T[K] } makes all properties optional (this is how Partial<T> is implemented). Removing uses the - prefix: { [K in keyof T]-?: T[K] } removes optionality (this is Required<T>). Similarly, -readonly removes readonly. This modifier manipulation is the foundation of many utility types.

Filtering Keys

You can filter which keys to include by adding a conditional type in the value position. If the condition is never, that key is excluded. For example, type StringKeys<T> = { [K in keyof T]: T[K] extends string ? K : never }[keyof T] extracts only the keys whose values are strings. This pattern, combined with Pick, lets you create types that select properties by their value type — a powerful technique for advanced type design.

Example

              // Basic mapped type — make every property a string.
type Stringify<T> = { [K in keyof T]: string };

interface User {
  id: number;
  name: string;
  isActive: boolean;
}

type StringUser = Stringify<User>;
// { id: string; name: string; isActive: string }

// Make all properties optional (this is how Partial<T> works).
type MyPartial<T> = { [K in keyof T]?: T[K] };

// Remove readonly (this is how Mutable<T> would work).
type Mutable<T> = { -readonly [K in keyof T]: T[K] };

// Make all properties readonly (this is how Readonly<T> works).
type MyReadonly<T> = { readonly [K in keyof T]: T[K] };

// Filter keys by value type — get only string-valued keys.
type StringKeyNames<T> = {
  [K in keyof T]: T[K] extends string ? K : never;
}[keyof T];

type StringFields = StringKeyNames<User>; // "name"

const partial: MyPartial<User> = { name: "Alice" };
const readonlyUser: MyReadonly<User> = { id: 1, name: "Bob", isActive: true };
console.log(partial, readonlyUser, StringFields);
            

Mapped types loop over keys with [K in keyof T]. Use + and - to add or remove readonly/optional modifiers. Conditional types in the value position filter which keys are included.

Try it

  • JSON to TypeScript

    Mapped types transform generated base types for specific use cases.

  • Code Diff

    Compare hand-written types with mapped-type-derived equivalents.

Common mistakes

The mistake

Confusing mapped types with index signatures

The fix

Mapped types [K in keyof T] iterate over known keys of T. Index signatures { [key: string]: V } allow any string key. They look similar but behave very differently.

The mistake

Forgetting that mapped types preserve key names but change value types

The fix

A mapped type { [K in keyof T]: NewType } keeps the same keys as T but replaces all value types with NewType. If you need to transform values selectively, add a conditional type.

Practice Mapped Types in TypeScript

4 exercises

Practice Mapped Types in TypeScript

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors