Readonly type under the hood in TypeScript

Example of Readonly use
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
Example of Mapped Types

Make values readonly

To make first layer of properties immutable, we use readonly modifier and that's it ✅

Solution
Solution

Check out the solution in Playground ⭐️

typescript

Alexey Berezin

Senior Software Engineer, UK Global Talent 🇬🇧

The portrait photo of me

Thank you for reading my blog post! If you have any questions, suggestions, or just want to say hi, feel free to reach out in social networks, or via email. I would love to hear from you!

Comments