13. What are mapped types in TypeScript, and how are they used?

Mapped types in TypeScript are a way to create new types based on existing ones. They allow modifying the properties of objects or other types according to specific rules. Example of using mapped types:

type Person = { name: string, age: number, address: string }; type ReadOnlyPerson = { readonly [K in keyof Person]: Person[K] }; // ReadOnlyPerson will have all properties of 'Person', but they will be readonly.

Mapped types are useful when we need to generate different variations of a type based on a base type, such as creating different versions of objects or states.

devFlipCards 2025

Do you accept cookies?

Cookies are small amounts of data saved locally on you device, which helps our website - it saves your settings like theme or language. It helps in adjusting ads and in traffic analysis. By using this site, you consent cookies usage.