- Now I just have a really simple example here
where we have a person, object
with an id, a name and an age.
Super simple and you can see all
of our code is working fine.
What happens if I wanna make it so
that I can't modify one of my properties?
So for example here, the id,
that's something I probably don't ever want to change
because the person shouldn't change their id.
But I can come in here and I can say the idea is now equal
to two and I'm not gonna get any errors thrown at all.
So in TypeScript they added a read-only attribute,
a property that you can add to any property
inside of an object or an interface or a type
and that's going to make sure that that specific property
is only going to be read only.
So whether you have a type or interface
that doesn't matter, they work exactly the same.
All you do is you just prefix the thing that you want
to make read only with the keyword readonly.
And now if I try to modify that id,
it just essentially says, Hey we can't assign
to this because it's a read only property.
Now technically this is valid JavaScript code
down here because I can redefine it if I want,
but by telling TypeScript that I don't want to
allow this to be redefined,
it's going to make sure it throws an error
anytime I try to compile or an error inside of
my editor to make me aware that I marked this
as read only for a reason and I'm going against that.
So it's obviously something wrong with either
my code or how I'm thinking about this problem.
So you can use read only here inside of just
essentially a type or a interface,
but you can also use it with an array as well.
So for example,
let's say I'm just gonna create a brand new type here
and this is just going to be number array
and that is going to be equal to a number array.
Wow, could you have guessed that?
But I'm gonna mark this as a read only number array.
Now I can come down here and I can create an array.
We'll just call it nums
and this is gonna be our number array.
We're gonna say it's one, two, three.
Then if I try to say nums dot push
you'll notice it doesn't even let me.
There's no auto complete for that
and that's because this is a read-only array.
So this does not even exist on the type.
Also, if I try to like add a property into here,
for example I try to set this to three,
I'm again gonna get an error
cuz this is a read only array
so I can't make any modifications to it at all.
So anytime you wanna create an array that you can't change
in any way, a read only attribute's gonna be a great way
for you to do that.
And the really nice thing is it just removes all
the different methods you could possibly use
from your auto complete.
As you can see, there's nothing inside of here
that allows me to actually modify the array.
It leaves in all of the methods that manipulate the array
without actually changing anything by returning a new array,
for example, for each or a map
they just loop through the array and return a new array.
They don't actually make changes to the array.
So it's a really good way to make sure
that you don't manipulate something that you
don't actually want to have manipulated.