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

Combining Multiple Remapping Operations

Chain multiple transformations by composing mapped types with different as clauses.

Combining Multiple Remapping Operations

What you'll learn: How to chain multiple mapped type transformations by composing different as clauses to create more powerful type manipulations.


You've learned how to rename keys in a single mapped type using the as clause. But what if you need to apply multiple transformations to the same base type? Perhaps you want to filter certain keys, rename others, and add prefixes—all in one cohesive type transformation.

The Composition Approach

TypeScript doesn't allow multiple as clauses in a single mapped type. Instead, you compose mapped types by nesting or chaining them—feeding the output of one mapped type as the input to another.

Think of it like a factory assembly line: each station (mapped type) performs one specific transformation, and the product moves through multiple stations to achieve the final result.

Basic Composition Pattern

// First transformation: filter to only number properties
type OnlyNumbers<T> = {
  [K in keyof T as T[K] extends number ? K : never]: T[K]
};

// Second transformation: add "get" prefix to remaining keys
type AddGetter<T> = {
  [K in keyof T as `get${Capitalize<K & string>}`]: T[K]
};

// Compose them together
type User = {
  id: number;
  name: string;
  age: number;
};

type Step1 = OnlyNumbers<User>; // { id: number; age: number }
type Step2 = AddGetter<Step1>;  // { getId: number; getAge: number }

// Or chain in one line
type Composed = AddGetter<OnlyNumbers<User>>;

You can apply three, four, or more transformations by continuing the chain. Each mapped type focuses on one job, making the overall transformation easier to understand and maintain.

Key Takeaway: Compose multiple remapping operations by chaining mapped types—the output of one becomes the input to the next, creating sophisticated type transformations through simple, focused steps.