0Pricing
TypeScript Academy · Lesson

Modifiers in Mapped Types: readonly and ?

Add or remove modifiers with +/- in mapped types.

Welcome

Mapped types can add or remove `readonly` and optional (`?`) modifiers from properties using the `+` and `-` prefixes.

Adding Modifiers with +

The `+` prefix (implicit by default) adds a modifier to each property in the mapped type.
type Readonly<T> = { +readonly [K in keyof T]: T[K] }; // + is implicit
type Optional<T> = { [K in keyof T]+?: T[K] };

All lessons in this course

  1. Mapped Types: Transforming Object Types
  2. Modifiers in Mapped Types: readonly and ?
  3. Template Literal Types: String Patterns
  4. Key Remapping with as in Mapped Types
← Back to TypeScript Academy