Sub topic 8
Renaming Object Keys with `as` Clauses
This chapter assumes you know basic TypeScript types, string literal unions, generics, and mapped types such as { [K in keyof T]: ... }. You should also know that keyof T produces a union of an object's property names.
Why rename keys in a mapped type?
A mapped type transforms every property in an existing type. By default, the output keeps each original property name:
type Optional<T> = {
[K in keyof T]?: T[K];
};
type Person = {
name: string;
age: number;
};
type OptionalPerson = Optional<Person>;
// { name?: string; age?: number }Sometimes the output needs different names. For example, an object with data properties might need a matching set of getter methods:
name -> getName
age -> getAgeA mapped type can rename a key with an as clause.
The `as` clause
The general form is:
type Renamed<T> = {
[K in keyof T as NewKey<K>]: T[K];
};The part before as chooses the source keys. The expression after as produces the output key. The value type can still refer to the original key with T[K].
For string keys, intersect keyof T with string:
type Getters<T> = {
[K in keyof T & string as `get${Capitalize<K>}`]: () => T[K];
};keyof T & string means “the keys of T that are strings.” This constraint is needed because Capitalize and template literal types operate on strings.
flowchart TD
A["Person: name: string, age: number"] --> B["K = name"]
A --> C["K = age"]
B --> D["getName: () => string"]
C --> E["getAge: () => number"]A complete getter example
A template literal type builds each new key. Capitalize<K> changes the first character to uppercase.
type Getters<T> = {
[K in keyof T & string as `get${Capitalize<K>}`]: () => T[K];
};
type Person = {
name: string;
age: number;
};
type PersonGetters = Getters<Person>;
const personGetters: PersonGetters = {
getName: () => "Ada",
getAge: () => 36,
};
console.log(personGetters.getName());
console.log(personGetters.getAge());
// This would fail because the generated key is getName, not name:
// const wrong: PersonGetters = { name: () => "Ada", age: () => 36 };The mapped type visits the name and age keys separately. For name, the new key is getName, and the value type is () => Person["name"], which is () => string. For age, the value type is () => number.
| Source key | Generated key | Generated value type |
|---|---|---|
| `name` | `getName` | `() => string` |
| `age` | `getAge` | `() => number` |
Filtering keys with `never`
An as clause can also remove keys. If the remapped key is never, TypeScript leaves that property out of the result.
For example, this type keeps only function-valued properties:
type FunctionsOnly<T> = {
[K in keyof T as T[K] extends (...args: any[]) => unknown ? K : never]: T[K];
};
type Service = {
start: () => void;
version: string;
stop: () => void;
};
type ServiceMethods = FunctionsOnly<Service>;
// { start: () => void; stop: () => void }The conditional type tests each property. start and stop produce their original keys. version produces never, so it is filtered out.
A common error is to use string instead of keyof T & string:
type BrokenGetters<T> = {
[K in keyof T as `get${Capitalize<K>}`]: () => T[K];
};This fails because K might be a number or symbol. Use keyof T & string when string operations are part of the new key.
Choosing a remapping strategy
Use an as clause when the output keys follow a predictable relationship to the input keys. It keeps the transformation connected to the source type, so added properties automatically receive generated keys.
Use a separate explicit type when names do not follow a rule. A type such as { loadUser: ..., saveUser: ... } is clearer when the output names cannot be computed consistently.
Exercise
Exercise
What type does Prefixed<{ id: number; title: string }> produce if Prefixed<T> maps each string key K to data_${K} while preserving its value type?
Show answer
It produces { data_id: number; data_title: string }.