Course contentsShow
← TypeScript
Lesson 721 of 1,79617. Mapped TypesFree lesson

Your First Mapped Type: Stringify

Building a type that transforms all properties of an object to string type.

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 T extracts all property keys from T as a union
  • [K in keyof T] iterates over each key
  • : string assigns the string type 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.