0Pricing
TypeScript Academy · Lesson

Mapped Types

Learn how to transform existing types into new ones using mapped types, providing flexibility in handling dynamic data structures.

Mapped Types is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 3. 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, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Introduction to Mapped Types

Mapped types allow you to transform existing types into new ones by applying operations to each property in a type.

They are powerful for creating flexible, reusable type structures in TypeScript.

Mapped Types — illustration 1

2

What are Mapped Types?

Mapped types use syntax like { [P in keyof T]: T[P] } to iterate over the properties of a type T.

3

Basic Example of Mapped Types

Here’s a simple example of how to use a mapped type to create a read-only version of a type:

type User = {
  name: string;
  age: number;
};

type ReadonlyUser = {
  readonly [P in keyof User]: User[P];
};

4

Using Built-in Mapped Types

TypeScript provides several built-in mapped types like:

  • Readonly<T>: Makes all properties of T read-only.
  • Partial<T>: Makes all properties of T optional.
  • Required<T>: Makes all properties of T required.
type User = {
  name: string;
  age: number;
};

// Example of Partial
const user: Partial<User> = {
  name: 'John',
};

5

Mapping Over Unions

Mapped types can also be used with union types to create new, combined types:

type Status = 'active' | 'inactive';

type StatusFlags = {
  [K in Status]: boolean;
};

const flags: StatusFlags = {
  active: true,
  inactive: false,
};

6

Conditional Mapped Types

You can use conditional types with mapped types to create more complex type transformations.

type User = {
  name: string;
  age?: number;
};

type NonOptional<T> = {
  [K in keyof T]-?: T[K];
};

type RequiredUser = NonOptional<User>;

7

Advanced Example: Exclude Properties

Here’s how you can create a mapped type to exclude certain properties:

type User = {
  id: number;
  name: string;
  password: string;
};

type PublicUser = {
  [K in keyof User as Exclude<K, 'password'>]: User[K];
};

8

9

Practice Task

Create a mapped type that adds a readonly modifier to all properties of a Product type. Test it by declaring a variable of the new type.

10

Congratulations!

You’ve learned how to use mapped types to transform existing types into new ones dynamically. Practice these techniques to make your TypeScript code even more powerful!

Mapped Types — illustration 10

Frequently asked questions

Is the “Mapped Types” lesson free?

Yes — the full text of “Mapped Types” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. 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.

What will I learn in “Mapped Types”?

Learn how to transform existing types into new ones using mapped types, providing flexibility in handling dynamic data structures. 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; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Mapped 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. Keyof and Lookup Types
  2. Mapped Types
  3. Intersection and Union Types
← Back to TypeScript Academy