If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
Here we have a map type that takes an input type T and makes all of its members as required using the minus question mark map type modifier To demonstrate this transformation, we have a type partial point where both X and Y are optional. If you pass this type T into the required map type, the output type will have both members X and Y as required. This kind of transformation is something that we need quite commonly, which is why it is a part of the built-in TypeScript library definitions.
00:33
So if we remove our required implementation, it falls back to the built-in one, and when we hover over it, you can see that it matches what we wrote by hand. Now let's look at a practical example of this required map type utility. Here we have a type circle config that has a few optional members, color and radius. And the objective here is that when someone wants to create a circle, they can provide these optional members or leave them out, and we will default them to the values that we want. Now within the circle, instead of having to deal
01:06
with these optional members all over the place, what we can do is that we can create a private config where all of these members are marked as required using the required built-in type utility. Then within our constructor, we take these optional members, and when initializing the private config, we ensure that they have default values and now anywhere else in our code, if you want to use the config, we can use the private config value without having to throw in any additional null checking.