0Pricing
TypeScript Academy · Lesson

Key Remapping with as in Mapped Types

Rename keys during type transformation.

Welcome

TypeScript 4.1 added the `as` clause in mapped types, allowing you to remap (rename) keys as part of the type transformation.

Basic Key Remapping

Add `as NewKeyType` after the key variable to remap to a new key name.
type Prefixed<T> = {
  [K in keyof T as `prefix_${string & K}`]: T[K]
};
type P = Prefixed<{ name: string }>;
// { prefix_name: string }

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