- This next built-in type is really simple.
It's very similar to required and partial
in that it just takes an entire object or type
and it just manipulates the entire type
to be one specific thing.
Imagine we wanted to have like a final to do
and this to do is one that cannot be changed.
It's the final to do.
It's like saved as is.
Essentially, it's read only.
Well, we can use a built-in read only type to do that.
So we can say read only and pass it in our type,
which is to do.
And now, when we hover over here,
you can see it marks every single property as read only.
This is very similar to how as const works,
but as const can only be used on JavaScript code
and it cannot be used on TypeScript code.
So I could say here that I have a to do,
which is going to be of type to do
I'm gonna set that equal to a to to do here real quick.
And our completed is false.
And now, if I hover over this,
you can see it has that type of to do.
Well, what I could do is I could set that as const,
and now essentially, that's going to give me
a read only version of my to do.
I can just remove this typing here from it.
And you can see, we get a read only version of this to do
by using this as const property.
It's the exact same thing as what happens
when I use read only on the type itself.
So if you wanna get the type of to do,
I can even say here, type test equals type of to do,
you can see that this test type right here
is exactly the same as this final to do.
They both do the exact same thing,
as const is just what you use when you have JavaScript code
and read only is what you have
when you want to create a type
from another type that you want to make read only.
I'm gonna be honest,
this isn't one that I find myself using too terribly often,
but for example, there's a method in objects
called object.freeze,
and essentially, this makes an object immutable,
so you can't change anything inside of it.
And this essentially just returns to you
a read only version of your type,
so it's using that read only type
to essentially define this if we can look at this.
And if I scroll down a little ways
to this freeze method here,
here's a really simple version of it.
You can see right here,
it just returns a read only version of your object,
so this is a really good use case
for where that would be something that you would want to do.
So again, it's not something you use all the time,
but if you want to convert one type to a read only version
of that same exact type,
the read only built-in type is perfect.