Intrinsics with Mapped Types
You'll learn how to combine TypeScript's built-in string manipulation types with mapped types to systematically transform every key in an object type.
The Power of Combining Forces
You've seen intrinsic string manipulation types like Uppercase<T> work on individual string literals. You've also seen mapped types iterate over object keys. Now combine them: use intrinsic types inside the key remapping clause of a mapped type to transform all property names at once.
This lets you create types that automatically convert entire object shapes — like converting all keys to uppercase, lowercase, or capitalized forms — without manually listing each property.
The Pattern
The syntax combines as key remapping with intrinsic types:
type UppercaseKeys<T> = {
[K in keyof T as Uppercase<K & string>]: T[K]
}
type ApiResponse = {
userId: number;
userName: string;
isActive: boolean;
}
// All keys become UPPERCASE
type ShoutingApi = UppercaseKeys<ApiResponse>;
// Result: { USERID: number; USERNAME: string; ISACTIVE: boolean }
The K & string intersection ensures we're working with string keys (since keyof can include symbols or numbers).
Real-World Applications
This pattern shines for enforcing naming conventions:
type PascalCaseKeys<T> = {
[K in keyof T as Capitalize<K & string>]: T[K]
}
type SnakeCaseKeys<T> = {
[K in keyof T as Lowercase<K & string>]: T[K]
}
type dbRecord = {
FirstName: string;
LastName: string;
}
type normalized = SnakeCaseKeys<dbRecord>;
// Result: { firstname: string; lastname: string }
You can also chain transformations or apply them conditionally within the mapped type logic.
Key Takeaway: Intrinsics with mapped types let you systematically transform object keys by combining string manipulation utilities with property iteration, creating powerful convention-enforcement and transformation types.