Transforming Property Types

Mapped types can also transform the types of the properties of an existing type, allowing for comprehensive type manipulation.

Syntax:

type PropertyTypeTransform<T> = {
[P in keyof T]: T[P] extends infer R ? Transform<R> : never;
};

Example: Converting all string properties of a type to boolean flags. Here, StringToBoolean is a mapped type that transforms all string properties of the Person type into booleans.

Javascript
type User = {
    id: number;
    name: string;
    email: string;
    verified: boolean;
};

// Transform all string properties to boolean properties
type StringsToBooleans<T> = {
    [P in keyof T]: T[P] extends string ? boolean : T[P];
};

function transformStringsToBooleans<T>(user: T): 
    StringsToBooleans<T> {
    let result = {} as StringsToBooleans<T>;
    for (const key in user) {
        if (typeof user[key] === 'string') {
            result[key] = true; 
            
            // Transform string to boolean true for 
            // demonstration
        } else {
            result[key] = user[key];
        }
    }
    return result;
}

const user: User = {
    id: 1,
    name: "Alice",
    email: "alice@example.com",
    verified: false,
};

const transformedUser = 
        transformStringsToBooleans(user);
console.log(transformedUser);

Output

"Name: Bob, Age: 30" 
"Attempting to modify a read-only person object..."
"Name: Bob, Age: 30"

Different ways to Create a TypeScript Mapped Type Utility Type

Mapped types in TypeScript are a powerful and flexible feature that allows developers to create new types by transforming existing ones. These types enable you to iterate through the keys of an object type and construct a new type based on the original type’s keys and values. This feature is particularly useful for creating utility types that can modify properties of an existing type in a DRY (Don’t Repeat Yourself) manner, enhancing code maintainability and type safety. Mapped types in TypeScript can be created using various techniques, each serving different purposes and use cases which are as follow:

Table of Content

  • Basic Mapped Type
  • Making Properties Optional
  • Creating Read-Only Types
  • Transforming Property Types

Similar Reads

Basic Mapped Type

A basic mapped type allows you to iterate over the keys of an existing type and apply a transformation to its properties....

Making Properties Optional

Mapped types can be used to make all properties of a type optional, useful for creating types that allow partial object updates....

Creating Read-Only Types

Mapped types can also enforce immutability by making all properties of a type read-only....

Transforming Property Types

Mapped types can also transform the types of the properties of an existing type, allowing for comprehensive type manipulation....

Mapping Property Types to Union Types

Mapped types can also be used to transform the types of properties into a union of specified types. This approach is particularly useful for scenarios where you want to create a type that accepts multiple types for each property....

Contact Us