Practical Pattern: API Response Reshaping
What you'll learn: How to automatically convert snake_case API properties to camelCase TypeScript interfaces using key remapping.
The Problem
Many backend APIs return data in snake_case (e.g., first_name, created_at), but JavaScript conventions favor camelCase (firstName, createdAt). Manually creating both types is tedious and error-prone.
The Solution: Key Remapping
Key remapping with as lets you transform property names systematically. Combined with template literal types, you can convert entire API shapes automatically:
type SnakeToCamel<S extends string> =
S extends `${infer First}_${infer Rest}`
? `${First}${Capitalize<SnakeToCamel<Rest>>}`
: S;
type CamelCase<T> = {
[K in keyof T as SnakeToCamel<K & string>]: T[K]
};
This recursively splits snake_case keys at underscores, capitalizing each segment after the first.
Real-World Example
type UserAPIResponse = {
user_id: number;
first_name: string;
last_name: string;
created_at: string;
};
type User = CamelCase<UserAPIResponse>;
// Result: { userId: number; firstName: string; lastName: string; createdAt: string; }
Now your runtime transformation logic can be type-safe—you know exactly which camelCase properties exist after conversion.
Key Takeaway: Key remapping with template literals and recursive conditional types transforms API shapes into idiomatic TypeScript interfaces automatically, eliminating manual duplication and keeping types synchronized with your data transformation code.