If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
As far as built-in utility types and types strip go, this type is not actually one that is often used by developers, but it is critical in how a number of popular open source libraries work, including those in the React and the view ecosystem. So let's take a look. We create these utility math functions, which are designed to be used attached to other objects. First, we define the type of this utility and then we create this utility as a simple object containing the two members, which is double and half. And of course, because we intend them to be attached to an object in order to function, we have to annotate
00:33
what this is going to be. And here we are annotating that this is something that is going to have a member value of type number. So of course we can attach it to an object that has a value of type number, for example, a value of one, and of course objective double is going to work fine. It is going to double the object of value. And similarly object do half is going to work as well. It is going to half that two back down to a one. Now, in both of these functions, we have to actually annotate what we expect this to be. Now within an object if you don't want to have to annotate this again
01:05
and again, there is a special utility type built into TypeScript called this type, which can be used to get rid of this duplicate annotation. Here we are saying that the math object is something that is going to have everything that is specified by the math type. And additionally, all usages of this within this object is going to point to something that has value of type number. And we can verify that by hovering over any of the instances of this. And you can see that it is indeed value of type number. Any usages of this beyond value of type number are going to result in a compiled time error.
01:39
Now, this type utility is actually different from some of the other utilities that are built into TypeScript in that it is actually intrinsic. It does not have a definition body. If you navigate to the definition, you can see that it is actually empty. And this is something that is actually baked into the brain of the TypeScript compiler, that this particular type means something special. This particular type is something that is used to define the definition of this within a particular object. Now, not having to annotate this again and again is definitely a good use case for the utility. However, it shines even more for describing a number
02:12
of popular libraries that exist within the JavaScript ecosystem. For example, a number of react state managers, as well as it is actually a core part of VJS. So let's demonstrate that by building a simple function that follows a similar API. For us, the definition of a state will consist of a data object along with some methods. And ha, we are annotating using the this type utility that the usage of this within the methods is going to have everything that exists within data as well as access to any of the other methods that might exist. Next, you provide a utility, create state function,
02:44
which takes a particular state description and then basically squashes the data as well as the method members into a single object, and therefore it has a return type penetration of D intersection with M. Now that's actually it for the definition of our library. Let's demonstrate How you would use it. For example, if you wanted to create a particular state with data containing X and Y of type number, and with a method move by that takes a DX and DY and modifies X and Y members, we can do that by calling create state and on the state object that is returned. We have access to X as well as Y, as well
03:16
as the move by method. Now, let's break down what is really happening over here a bit more. We are squashing data and methods into a single object, which means that this within the methods is now going to point to something that has access to both the data members as well as the methods members. And the annotation that we have of this type pointing to D as well as M is something that is enforcing that, which is why when we hover over this within the move by function, you can see that it has access to data as well as any methods. Any misusage over here is going to be caught
03:48
by TypeScript at compiled time. So thanks to the this type annotation, we get very easy access to data and we don't need to keep on going this data X and this Y. The impact of the squashing as a return type of create state is actually quite easy to define, but the impact of the squashing on the meaning of this within the methods is something that becomes very easy, only thanks to this type utility.