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 called partial that takes an input type T and makes all of its members optional using the question mark map type modifier to demonstrate its usage. We have a type point with two required members, X and Y, both of type number. If you pass this type into this partial map type, we get an output type where both X and Y become optional. This kind of a transform is something that we need quite commonly, which is why it is a part of the standard type script library definitions,
00:34
and you don't need to define it by yourself. So if you remove our definition, it falls back to the built-in one, and when we hover over it, you can see that it matches the one that we wrote by hand. Now let's demonstrate its usage with a practical example. Here we have a class state of type T, which takes an initial value and is constructed and provides a method called update where you can provide only the members that you want to change. And we annotate the parameter of the update method using the built-in partial map type utility.
01:07
What this means is that if we create a state with initial values X and Y of type number, we can use the update method to only update the Y member as an example, and there is no need to provide the X member. If you log out the current value, you will see that X remains consistent with the initial value of zero and only Y is updated.