Readonly under the hood

Example of Readonly use

Second challenge is Readonly

Similarly to Pick, itโ€™s usually used when you need to declare the type which is based on another type. The values on the first layer are immutable, or readonlyย ๐Ÿ”’

Iteration over an object

As previously for Pick, we use Mapped Types for the iteration:

Example of Mapped Types

Make values readonly

To make first layer of properties immutable, we use readonly modifier and thatโ€™s it โœ…

Solution

Check out the solution in Playground โญ๏ธ

typescripteasy

Let's chat and have fun ๐Ÿง‘โ€๐Ÿ’ป๐Ÿ‘ฉโ€๐Ÿ’ป

No more often than once a week I write ๐Ÿ“„ about TypeScript ๐Ÿ’ช, video playback ๐Ÿ“บ and frontend related topics. You can unsubscribe in any time โ†˜๏ธ

ยฉ 2021, Built with Gatsby