Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Type Operators

TypeScript mapping modifiers

Reference for mapping modifiers in TypeScript mapped types: add or remove readonly and optional with + and -, Required, Mutable, Partial and Readonly.

Inside a mapped type you can add or remove two property modifiers: readonly and ? (optional). Prefix the modifier with + to add it or - to remove it. With no sign, + is assumed.

A homomorphic mapped type ([K in keyof T]) copies the modifiers of the source property. The - form is how you strip them: -? makes every property required and -readonly makes it writable. The built-in Required<T>, Partial<T> and Readonly<T> are exactly these maps.

TypeScript
type Mutable<T> = { -readonly [K in keyof T]: T[K] };
type AllRequired<T> = { [K in keyof T]-?: T[K] };

Try it

TypeScript Add and remove modifiers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript The built-ins are modifier maps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
{ [K in keyof T]+?: T[K] }     // add optional
{ [K in keyof T]-?: T[K] }     // remove optional
{ +readonly [K in keyof T]: T[K] } // add readonly
{ -readonly [K in keyof T]: T[K] } // remove readonly
Modifier Effect
readonly or +readonly The property becomes read-only.
-readonly Removes read-only.
? or +? The property becomes optional (T | undefined unless exactOptionalPropertyTypes is on).
-? The property becomes required, and undefined that was added by ? is removed.

Edge cases: -? removes undefined from the property type only when the undefined came from the optionality itself. With exactOptionalPropertyTypes the distinction is tracked precisely.

Usage notes

  • Only homomorphic maps inherit modifiers, so -? and -readonly need keyof T (or a key type constrained to it) as the key source.
  • Required<T> also strips undefined from properties that were only optional, but keeps an explicit | undefined declared in the type when exactOptionalPropertyTypes is on.
  • Readonly is shallow. Readonly<T> does not protect nested objects; write a recursive map for that.
  • Readonly is not runtime protection. It is a compile-time check; use Object.freeze for runtime immutability.
  • Assignment compatibility: readonly properties are assignable to mutable ones (the check is not variance-sound), so do not rely on it for safety across function boundaries.
  • Mixed changes (some keys optional, others not) are built from an intersection of two maps or with Pick/Omit.