Ch. 2 · TypeScript

TypeScript Mapped Types and Modifiers

TypeScript Mapped Types and Modifiers. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readintermediateupdated Oct 3, 2026

Mapped types transform a set of keys. They can preserve or change optional and readonly modifiers while retaining relationships with original property types.

Before you start

You should be comfortable with JavaScript values, functions and objects. Use a TypeScript project with strict checking enabled when trying the examples. Separate what the compiler proves from what must still be checked when values arrive at runtime.

The practical goal is to reason through this situation: Create a mutable variant with -readonly or require every key with -?. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Identify the keys to transform

A mapped type visits known keys and derives each property’s type from the original. It changes a description, not an object.

Step 2: Choose modifier changes

Remove readonly with -readonly or optionality with -?. Keep other property relationships intact instead of replacing every value with any.

Step 3: Construct a valid runtime result

Supply or validate missing fields before returning a complete value. An assertion alone cannot materialize absent properties.

Worked scenario

Create a mutable variant with -readonly or require every key with -?.

type Complete<T> = { [K in keyof T]-?: T[K] };
type Settings = { theme?: string; pageSize?: number };
function complete(input: Settings): Complete<Settings> {
  return {
    theme: input.theme ?? 'light',
    pageSize: input.pageSize ?? 24,
  };
}
TypeScript

The type describes a fully populated result. The function performs the work that makes it true, preserving explicit zero rather than using a truthiness fallback.

Common mistake

Making fields required does not create missing values at runtime.

Verify the behavior

Call complete with neither field, one field and both fields. Decide whether zero is valid or should fail domain validation; defaulting and validation solve different problems.

Interview exercise

Convert a partial record to a complete one.

Answer and reasoning

Validate and fill defaults before returning the required type; the mapped type only describes the completed result.

Continue learning

Compare the scenario with the TypeScript interview questions and test your understanding with the TypeScript MCQs. For terminology and implementation details, consult the reference material.

More in TypeScript

read ✓TypeScript · hard

TypeScript Abstract Classes and Contracts

Share behavior with abstract classes, enforce required members, and decide when an interface or composition is the better contract.

~2 min readread →
read ✓TypeScript · mid

TypeScript Enums vs Union Types

Compare enums with unions of string literals: runtime cost, serialization, exhaustiveness and which one fits application code.

~3 min readread →
esc