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 read only using the read only map type modifier. To demonstrate this transform, we have a type point with members X and Y, both of which are editable. When we pass this type into the map type, the output type will have both X and Y marked as read only, and we can see that when we hover over the output type. This kind of a transform is also something that is used quite commonly in the libraries out there,
00:32
which is why it is a part of the built-in type script library definitions. So if you delete our implementation, it falls back to the built-in one, and when we hover over it, you can see that it is same as the one that we wrote by hand. Now let's demonstrate this built-in type with a practical example. Here we have a function called make read only. That takes an object of type T and returns an object where all the members are marked as readonly within the function body. It creates a shallow copy of the object and uses the built-in JavaScript runtime function called object freeze to freeze this object
01:05
essentially making it read only. Now we have an editable point with members X and Y, and you can see that we are allowed to modify its members, for example, set the X to a new value. However, we can make a read only version of this point by passing it to the make read only utility function. Because of the read only return type annotation transcript understands that this point would be read only, and if you try to modify its values, for example, the X member, we get a compiled time error.