TypeScript's key remapping (the as clause in mapped types) lets you transform every key while preserving the value types:
// Prefix all keys with a string:
type Prefixed<T, P extends string> = {
[K in keyof T as `${P}${Capitalize<string & K>}`]: T[K]
};
// Convert each property into a getter method:
type Getterized<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K]
};
// Invert keys ↔ values:
type Inverted<T extends Record<string, string>> = {
[V in T[keyof T]]: keyof T
};Implement three runtime utilities:
| Function | Example |
|---|---|
prefixKeys(obj, 'data_') | { name: 'Alice' } → { data_name: 'Alice' } |
invertObject(obj) | { a: 'x', b: 'y' } → { x: 'a', y: 'b' } |
toGetterObject(obj) | { count: 5 } → { getCount: () => 5 } |
Sample tests