Course contentsShow
← TypeScript
Lesson 718 of 1,79617. Mapped TypesFree lesson

The Basic Mapped Type Syntax

Learning the [K in keyof T]: ... syntax for creating new types by mapping over existing ones.

The Basic Mapped Type Syntax

You'll learn the foundational [K in keyof T]: ... syntax that lets you transform existing types into new ones by iterating over their properties.

Mapped types are TypeScript's way of creating new object types by transforming each property from an existing type. Think of them like JavaScript's Array.map(), but for type definitions instead of runtime values.

The Core Syntax

The basic structure looks like this:

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

Let's break down each piece:

  • [K in keyof T] — This is the "mapping" part. It says "for each key K in the type T..."
  • K — A type variable representing each property name as you iterate
  • in keyof T — Loops over all keys in type T (remember keyof extracts property names as a union)
  • : T[K] — The type assigned to each property (here, we're keeping the original type by looking it up)

A Simple Example

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

// Creates an exact copy of User
type CopiedUser = {
  [K in keyof User]: User[K];
};

// CopiedUser is identical to:
// { name: string; age: number; }

This mapped type iterates over "name" and "age", and for each one, assigns it the same type from User.

Why This Matters

While copying a type isn't useful, this syntax becomes powerful when you change something during the mapping — exactly what utilities like Partial, Readonly, and Pick do under the hood.

Key Takeaway: The [K in keyof T]: ... syntax is TypeScript's way of iterating over an object type's properties to create a new, transformed type — it's the foundation of all mapped type patterns.