Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TypeScript · Function

Mapped Types

Mapped types transform an existing type by iterating over a union of property keys, commonly keyof another type. They can change property values, modifiers, and—using key remapping—property names.

How it works

A mapped type creates a new type by iterating over a key union, commonly obtained with keyof, and assigning a transformed property type to each key. The mapped declaration can also preserve, add, remove, or conditionally rename properties while retaining their relationship to the source type.

Syntax

type Mapped<Type> = {
  [Property in keyof Type]: NewType;
};

Parameters

NameTypeDescription
Typegeneric type parameterThe source type whose keys and property types are transformed.

Returns

A new object type whose properties are produced by iterating over the selected keys.

Key points

  • Mapped types iterate over a key union and construct a related output type.
  • keyof is a common way to obtain the source type's property-key union.
  • A mapped value expression can uniformly replace every source property's value type.
  • Mapped declarations can control mutability and optionality in addition to value types.
  • The plus modifier is implicit, so an unprefixed readonly or optional marker adds that modifier.
  • Mapped types can iterate over unions beyond the usual property-key union in supported transformations.

Common mistakes

  • Mapped properties may carry readonly and optional modifiers; use + or - prefixes when you need to explicitly add or remove those modifiers.
  • Key remapping is available through an as clause, so the output keys need not be identical to the input keys.
  • A remapped key of never removes that property from the resulting type.

Example: Map feature members to flags

type Features = {
  darkMode: () => void;
  newUserProfile: () => void;
};

type OptionsFlags<Type> = {
  [Property in keyof Type]: boolean;
};

type FeatureOptions = OptionsFlags<Features>;

const enabled: FeatureOptions = {
  darkMode: true,
  newUserProfile: false,
};

The mapped type transforms every property in Features into a boolean property.

When to use it

Generate boolean flags

Create a configuration type whose entries indicate whether corresponding features are enabled.

Change optionality

Derive an editable or required variant without duplicating every property declaration.

Remap and filter keys

Generate renamed properties or omit selected members while deriving an API surface.

Interview questions

QWhat is a mapped type, and what does keyof commonly contribute?

A mapped type iterates over a union of PropertyKey values, often keyof Type, and creates one output property per key. Its value expression can transform each source property, such as changing every value to boolean.

Follow-up: How can a mapped type remove selected properties? Use key remapping with an as clause and produce never for keys that should be excluded.

QHow do mapped types control readonly and optional properties?

The modifiers are readonly and ?. A modifier without a prefix means +, while - removes the corresponding modifier, for example -? makes mapped properties required.

Follow-up: How would you make every property mutable and required? Apply -readonly and -? in the mapped declaration.

QHow do key remapping and filtering work?

An as clause remaps each source key to a new key type. Conditional logic can return never to filter a key, and template literal types can derive names from existing keys.

Follow-up: What constraint is useful when remapping only string keys? A conditional such as Key extends string ? ... : never excludes number and symbol keys.

  • keyof
  • index signatures
  • conditional types
  • template literal types
  • Partial
  • Readonly

Source: TypeScript: Documentation - Mapped Types. Checked 2026-10-15.