Your First Mapped Type: Stringify
You'll learn: How to build a mapped type that transforms every property in an object to the string type.
The Problem
Imagine you have an object type with various properties of different types. Sometimes you need a version where all those properties become strings—perhaps for serialization, form state, or API payloads. Writing out each property manually would be tedious and error-prone.
Building Stringify<T>
Here's your first practical mapped type:
type Stringify<T> = {
[K in keyof T]: string;
};
This reads as: "For each key K in the keys of T, create a property with that same key, but make its type string."
Breaking It Down
keyof Textracts all property keys fromTas a union[K in keyof T]iterates over each key: stringassigns thestringtype to every property, regardless of its original type
Using Stringify
type User = {
id: number;
name: string;
isActive: boolean;
};
type UserForm = Stringify<User>;
// Result:
// {
// id: string;
// name: string;
// isActive: string;
// }
Notice how id changed from number to string, and isActive from boolean to string. The property names stayed the same, but their types were uniformly transformed.
Key Takeaway: Mapped types let you iterate over object keys with [K in keyof T] and systematically transform property types—Stringify is the simplest example, replacing all types with string.