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. Create Quiz Comment I iamgaurav Follow 0 Improve I iamgaurav Follow 0 Improve Article Tags : TypeScript Explore TypeScript Tutorial 8 min read TypeScript BasicsIntroduction to TypeScript 3 min read Difference between TypeScript and JavaScript 4 min read How to install TypeScript ? 3 min read Hello World in TypeScript 2 min read How to execute TypeScript file using command line? 2 min read Variables in TypeScript 6 min read What are the different keywords to declare variables in TypeScript ? 4 min read Identifiers and Keywords in TypeScript 2 min read TypeScript primitive typesData types in TypeScript 3 min read TypeScript Numbers 4 min read TypeScript String 4 min read Explain the concept of null and its uses in TypeScript 3 min read TypeScript Object typesWhat are TypeScript Interfaces? 4 min read TypeScript class 4 min read How enums works in TypeScript ? 4 min read TypeScript Tuples 4 min read TypeScript other typesWhat is any type, and when to use it in TypeScript ? 3 min read How to Create an Object in TypeScript? 4 min read What is an unknown type and when to use it in TypeScript ? 3 min read Explain the purpose of never type in TypeScript 3 min read TypeScript combining typesTypeScript Union 3 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript AssertionsExplain Type assertions in TypeScript 3 min read TypeScript FunctionsHow to write a function in Typescript ? 4 min read How to achieve function overloading in TypeScript ? 2 min read Explain the arrow function syntax in TypeScript 2 min read TypeScript toPrecision() Function 1 min read TypeScript toFixed() Function 2 min read TypeScript toLocaleString() Function 2 min read TypeScript toString() 1 min read TypeScript interfaces and aliasesWhat are TypeScript Interfaces? 4 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript classesHow to Extend an Interface from a class in TypeScript ? 2 min read How to Create an Object in TypeScript? 4 min read How to use getters/setters in TypeScript ? 5 min read TypeScript Inheritance 3 min read When to use interfaces and when to use classes in TypeScript ? 4 min read Generics Interface in typescript 5 min read How to use property decorators in TypeScript ? 4 min read Like