Skip to content

Mapped Modifiers

The Course
•
25 min study

Intermediate introduced a readonly mapped type. Now read the modifiers explicitly: -readonly removes a readonly permission restriction, and -? removes an optional-property marker. Neither expression creates or modifies a JavaScript object.

For a configuration type with optional fields, RequiredFields<T> can require callers to supply all fields. The runtime implementation still needs defaults or validation if data arrives incomplete. A declaration cannot fill missing values in JSON.

Do not confuse making a writable view with obtaining independent ownership. Two variables can still refer to the same object. Use a new object when a transformation promises a copy. Prefer built-in Required, Readonly and Partial in ordinary application code; these small definitions teach you to read the underlying syntax.

Keep transformations shallow. If a field contains a nested record, removing readonly at the outer level does not rewrite that nested record's type. Test the exact caller behavior your API needs instead of assuming every modifier propagates recursively.

Predict, check, and run

Write the output before running. Use Check types first, then Check & run. Types disappear at runtime: explain both what the compiler checks and what the code actually does. Each editor is an independent ES module; await asynchronous work explicitly.

Predict, then runTypeScript
type RequiredFields<T> = { [K in keyof T]-?: T[K] };

type Writable<T> = { -readonly [K in keyof T]: T[K] };

type Options = {
  readonly title?: string;

  readonly limit?: number;
};

const options: Writable<RequiredFields<Options>> = { title: "Types", limit: 2 };

options.limit = 3;
console.log(options.title, options.limit);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
Types 3

Read the compiler error

Predict the rejected relationship before checking. Read the real diagnostic and its source location; fix the contract or the value instead of hiding it with an assertion.

Find and fix the type errorTypeScript
type Complete<T> = { [K in keyof T]-?: T[K] };

const settings: Complete<{
  label?: string;

  enabled?: boolean;
}> = {
  label: "Types"
};

console.log(settings);

Enable JavaScript to edit and type-check this example.

Check your diagnosis

The key line is TS2741: Property 'enabled' is missing in type '{ label: string; }' but required in type 'Complete<...>'. The first type is your object; the second is the mapped type, and the word required tells you the -? modifier took effect.

Removing ? requires enabled to be present; no runtime default is inserted.

Permissions and required fields change independently

Removing optional markers changes what a caller must supply; removing readonly changes which assignments are allowed through a view. Neither fills missing runtime data or allocates another record. Compose those transformations only when both changes fit the public API.

An explicit undefined union is another choice. Required does not mean nonblank or positive, and writable does not mean independently owned. Test the exact caller operation you want to accept or reject rather than treating these modifiers as a general validation tool.

Permissions and required fields change independently: traceTypeScript
type Complete<T> = { [K in keyof T]-?: T[K] };

type Draft = {
  enabled?: boolean;

  minutes?: number;
};

const value: Complete<Draft> = { enabled: false, minutes: 0 };

console.log(value.enabled, value.minutes);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
false 0

Completeness requires both fields, while false and zero remain legitimate values. Optional markers alone said nothing about their domain meaning.

Permissions and required fields change independently: contrastTypeScript
type Writable<T> = { -readonly [K in keyof T]: T[K] };

const source: { readonly minutes: number } = { minutes: 20 };
const view: Writable<typeof source> = source;

view.minutes = 30;
console.log(source.minutes);
console.log(view === source);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
30
true

The writable view shares the original record. Removing an assignment restriction did not create a copy; a library must avoid advertising this as an isolated mutable draft.

Try the rule on new inputs

Compare Required, Readonly and a new spread record. Name which operation changes checking permissions, which requires fields and which creates new runtime identity. Test a nested field separately.

Debug a plausible mistake

Exercise

Repair a mapped modifier

Editable must allow top-level property reassignment and print Test. Preserve every property's value type. This is a type-level transformation, not a runtime clone or deep mutability operation.

Use Check types to inspect the genuine diagnostic before repairing the code. Keep the intended contract; do not bypass the checker with any, a broad assertion or an ignore comment.

Find and fix the type errorTypeScript
type Editable<T> = { [K in keyof T]: T[K] };
type Source = { readonly title: string };
const book: Editable<Source> = { title: "Read" };
book.title = "Test";

console.log(book.title);

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

Check types reports TS2540: Cannot assign to 'title' because it is a read-only property. The mapped type copied the readonly modifier from its input; the diagnostic names the property, not the mapped type, so trace which type gave title that modifier.

A mapped type preserves existing property modifiers unless explicitly changed.

Hint 2: a first step

Remove readonly in the mapped property declaration.

Show one solution
TypeScript
type Editable<T> = { -readonly [K in keyof T]: T[K] };
type Source = { readonly title: string };
const book: Editable<Source> = { title: "Read" };
book.title = "Test";

console.log(book.title);
Text
Test

A mapped type over keyof T copies each property's modifiers, so the original Editable kept readonly and the assignment failed with TS2540 "Cannot assign to 'title' because it is a read-only property." -readonly removes that modifier while T[K] keeps the value type string. The object itself was never frozen: this changes only what the checker allows through this type.

Trace the contract before building

Map over the original keys one at a time and ask which property modifiers the result preserves. A straightforward mapped type can retain optional and readonly properties. Removing readonly explicitly changes what callers may assign through the new type; it does not clone an object or unfreeze a runtime value.

Compare the source and target declarations using one originally required property and one originally optional property. Then try a write that should be accepted only by the transformed type. Keep runtime ownership separate: two references can still point to the same object even when they have different static views. A type transformation alone never creates an independent copy.

Build it yourself

Exercise

Mapped Modifiers: practice

Create Complete<T> removing optional markers and ReadonlyFields<T> adding readonly. Make a complete readonly settings record with label Types and enabled true. Print Types true; verify deleting enabled is a type error.

Your workspaceTypeScript
// Start from the stated contract. Add a valid call and an edge case.

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

Compose two small type transformations rather than pretending one clones data.

Hint 2: a first step

The optional modifier belongs after the closing key bracket; readonly belongs before it.

Show one solution
One possible solutionTypeScript
type Complete<T> = { [K in keyof T]-?: T[K] };

type ReadonlyFields<T> = { readonly [K in keyof T]: T[K] };

type Draft = {
  label?: string;

  enabled?: boolean;
};

const settings: ReadonlyFields<Complete<Draft>> = {
  label: "Types",
  enabled: true
};

console.log(settings.label, settings.enabled);

Enable JavaScript to edit and type-check this example.

Text
Types true

Read the nested type from the inside out: Complete<Draft> removes both ? markers, then ReadonlyFields adds readonly to the result. Leaving out enabled now fails with TS2741 "Property 'enabled' is missing", and settings.enabled = false fails with TS2540. Each modifier change is independent; swapping the order of the two wrappers gives the same type here.

Change it and test it

Try a missing enabled property and a nested mutable array. Explain the separate effects of completeness, readonly and copying. Compare with Readonly<Required<Draft>>.

Was this article helpful?

Your response is saved on this device.