Q34: What is Key Remapping in Mapped Types using the `as` clause?

Difficulty:AdvancedTopic:Mapped Types

Key remapping allows you to rename or filter out property keys during mapped type evaluation using an as clause.

Getter Method Key Remapping
type Getters<T> = {
  [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K]
};

interface Person {
  name: string;
  age: number;
}

type PersonGetters = Getters<Person>;
// Result: { getName: () => string; getAge: () => number; }
💡 The as clause remaps key names dynamically using template literal transformation.
Key Takeaways
  • Key remapping permits filtering and transforming property key names dynamically.