TypeScript Mapped Types Last Updated : 24 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Mapped types in TypeScript allow you to create new types by transforming the properties of existing types.They enable modifications like making properties optional, read-only, or altering their types.Mapped types help reduce code duplication and enhance type safety by automating type transformations.They are particularly useful for creating variations of types without manually redefining each property. JavaScript type User = { id: number; name: string; email: string; }; type PartialUser = { [P in keyof User]?: User[P]; }; PartialUser is a new type where each property of User is made optional.The keyof operator retrieves all keys from User, and the mapped type iterates over each key, marking them as optional with ?.Output:const user1: PartialUser = { id: 1 }; const user2: PartialUser = {}; const user3: PartialUser = { id: 2, name: "Alice" }; More Example of TypeScript Mapped TypesCreating Readonly Properties JavaScript type User = { id: number; name: string; email: string; }; type ReadonlyUser = { readonly [P in keyof User]: User[P]; }; const user: ReadonlyUser = { id: 1, name: "Alice", email: "[email protected]" }; user.id = 2; ReadonlyUser is a new type where all properties of User are marked as readonly.Attempting to modify any property of user will result in a compile-time error.Output:Error: Cannot assign to 'id' because it is a read-only property.Creating Nullable Properties JavaScript type Product = { name: string; price: number; inStock: boolean; }; type NullableProduct = { [P in keyof Product]: Product[P] | null; }; const product: NullableProduct = { name: "Laptop", price: null, inStock: true }; NullableProduct is a new type where each property of Product can be its original type or null.This allows properties to explicitly have a null value, indicating the absence of a value.Output:{ name: "Laptop", price: null, inStock: true }Renaming Properties with Template Literals JavaScript type Person = { firstName: string; lastName: string; }; type PrefixedPerson = { [P in keyof Person as `person${Capitalize<P>}`]: Person[P]; }; const person: PrefixedPerson = { personFirstName: "John", personLastName: "Doe" }; PrefixedPerson creates a new type by prefixing each property of Person with 'person' and capitalizing the original property name.This demonstrates how to transform property names using template literal types and the Capitalize utility type.Output:{ personFirstName: "John", personLastName: "Doe" }Best Practices for Using TypeScript Mapped TypesKeep Transformations Simple: Avoid overly complex nested transformations to maintain readability and ease of maintenance.Ensure Type Safety: Leverage mapped types to enforce consistent property transformations, enhancing type safety across your codebase.Combine with Utility Types: Utilize built-in utility types like Partial, Readonly, Pick, and Omit to simplify common transformations. Comment More infoAdvertise with us Next Article TypeScript Mapped Types I iamgaurav Follow Improve Article Tags : JavaScript Web Technologies TypeScript Similar Reads Data types in TypeScript In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim 3 min read TypeScript Generic Types TypeScript Generic Types can be used by programmers when they need to create reusable components because they are used to create components that work with various data types and this provides type safety. The reusable components can be classes, functions, and interfaces. TypeScript generics can be u 2 min read TypeScript Map TypeScript Map is a collection that stores key-value pairs, where keys and values can be of any type. It maintains the insertion order of keys and provides methods to add, retrieve, check, remove, and clear entries, ensuring efficient management of key-value data.Creating a MapA map can be created a 3 min read TypeScript Indexed Access Types TypeScript's Indexed Access Types allow you to access the type of a specific property within an object or array using bracket notation (Type[Key]). This feature provides precise type information about properties or elements, enhancing type safety when accessing deeply nested structures or specific o 3 min read TypeScript Conditional Types In TypeScript, conditional types enable developers to create types that depend on a condition, allowing for more dynamic and flexible type definitions.They follow the syntax T extends U ? X : Y, meaning if type T is assignable to type U, the type resolves to X; otherwise, it resolves to Y.Conditiona 4 min read Like