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
- Mapped Types: Transforming Object Types
- Modifiers in Mapped Types: readonly and ?
- Template Literal Types: String Patterns
- Key Remapping with as in Mapped Types