Partial Readonly
2021-04-23
We already solved the challenge with readonly (see Readonly under the hood). Today we want to apply readonly modifier only to the specified keys (Readonly 2).
Combination of 2 challenges
Again, as for previous challenge Omit under the hood, we have a combination of challenges that we already did:
- We already learnt how to apply readonly modifier to the keys:
- We also need to pick specified keys:
- And we need to have excluded keys without readonly modifier:
The algorithm is:
- Pick keys we want to make readonly
- Make picked keys readonly
- Add the rest (or excluded) keys without changes
If you check the current solution, you see that you cannot use Pick
and Omit
if K
is not part of keyof T
. Let's fix that:
But still we expect to have all keys by default if we don't specify K
. So the final solution is:
As usual, the solution is in Playground 💻
Have a nice weekend 🌤
typescript