TypeScript Academy · Lesson

Mapped Types: Transforming Object Types

Loop over keys to create new type shapes.

Lesson 1 of 413 steps

Mapped Types: Transforming Object Types is a free TypeScript Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 4 lessons in total.

Welcome

Mapped types iterate over the keys of a type to create new types with transformed properties. They are the foundation of Partial, Readonly, Record, and many other utilities.

Basic Mapped Type Syntax

Use `[K in keyof T]` to iterate over all keys of T and produce a new type.
type Copy<T> = { [K in keyof T]: T[K] };
type UserCopy = Copy<User>; // same as User

Making All Properties Optional

This is how Partial is implemented — add ? to each property.
type Partial<T> = { [K in keyof T]?: T[K] };
type PartialUser = Partial<User>;

Making All Properties Readonly

Add `readonly` to each property in the mapped type.
type Readonly<T> = { readonly [K in keyof T]: T[K] };
type FrozenUser = Readonly<User>;

Transforming Value Types

Change the value type for each property while keeping the keys.
type Stringify<T> = { [K in keyof T]: string };
type StringUser = Stringify<User>; // all values are string

Filtering Properties with never

Use a conditional in the value position and filter with never via key remapping.
type OnlyStrings<T> = {
  [K in keyof T as T[K] extends string ? K : never]: T[K]
};

Record<K, V>

Record creates an object type with specific keys and value types.
type Record<K extends string | number | symbol, V> = { [P in K]: V };
const roles: Record<'admin' | 'user', boolean> = { admin: true, user: false };

Mapped Types Preserve Optionality

By default, mapped types preserve the optional and readonly modifiers from the source type.
interface User { id: number; name?: string; }
type Copy<T> = { [K in keyof T]: T[K] };
type C = Copy<User>; // id: number; name?: string (preserved)

Key Remapping with as

Remap keys to new names using `as` in the mapped type.
type Getters<T> = {
  [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K]
};

Iterating Over a Union

Map over a union type instead of keyof T to create object types from unions.
type Flags = { [K in 'option1' | 'option2' | 'option3']: boolean };

Combining Mapped and Conditional Types

Mapped types and conditional types compose naturally to produce sophisticated transformations.
type NullableValues<T> = { [K in keyof T]: T[K] | null };

Quick Check

What does `{ [K in keyof T]?: T[K] }` produce?

Recap

Mapped types iterate over keys to create new types. They can add/remove readonly and ?, transform value types, filter keys with never, and remap keys with as. They power all of TypeScript's built-in utility types.
Free to start

Learn TypeScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
101
Lessons
352

Frequently Asked Questions

Is the “Mapped Types: Transforming Object Types” lesson free?

Yes — the full text of “Mapped Types: Transforming Object Types” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO. The TypeScript Academy course includes 4 lessons in total.

What will I learn in “Mapped Types: Transforming Object Types”?

Loop over keys to create new type shapes. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Mapped Types: Transforming Object Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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