- At the beginning of this course, we talked
about the any type and how it essentially means
that this variable can be anything.
TypeScript has no idea what it means
and it essentially just shuts off TypeScript
for everything related to that variable.
'Cause it doesn't know what it could be
and it doesn't have any type checking it can do.
Well, there's another type in TypeScript that's very similar
to any where TypeScript has no idea what it is.
But instead of allowing you to do anything that you want
it restricts you and prevents you
from doing anything with that variable
until you narrow it down to figure out what it actually is.
And that type is the unknown type.
If we come in here and we type this as unknown
you'll notice immediately when we try to
use this data variable, you can see the type is unknown.
So we can't do anything with it at all.
We need to use type narrowing.
So if we want to check to see
if this thing has a name property
well what we can do is we can say, Hey
if the type of our data is equal to an object
well we're one step of the way there.
Now we need to check to see if it has this name property.
'Cause right now, if we were to just put our code
inside of here and clean up all of our different errors
you'll see that right now it's not working
because it doesn't know
if this name property exists for our data object.
Also, it could be null.
So we need to check for that.
So we can say if the data is not equal to null
we'll come in here like that.
There we go.
It's not equal to null and the data is equal
to an object and the data has that particular key of name.
So to make that check, we can use the TypeScript check
of name in data just like that.
And that's just saying
that there is a name key inside of this data object.
Now it knows that I have this data object
which is a record that has this name property
and we don't know what this name property is yet.
If we wanted to, you know, do more with this name property
like determine that it's a string
by logging out the length of it
well we would have to add in an additional check
by checking that data.name is the type of a string.
There we go.
So now we have a bunch
of different checks to essentially determine
that this data object is an object that has a name property
which is a string.
Now I know if you're used to JavaScript
this is a whole lot of code to essentially do nothing.
But the reason we're doing all this is for type safety.
If you have a variable in your TypeScript code
and you don't know what the type of that is, typing it
as any and just ignoring it is a surefire way to
essentially go back to your old ways of JavaScript
and give up all of the benefits of TypeScript
because it only takes a couple places where you use any
for it to ruin your entire code base.
Because just one section that's not covered
by TypeScript means you can't trust pretty much any
of your application that touches that part of your code.
A unknown, on the other hand
requires a lot more upfront work
because we don't know anything about it
and TypeScript is incredibly strict with unknown.
So we have to do all of these different checks to make sure
that this thing is what we expect it to be.
But by doing all of that, we're essentially
forcing TypeScript to learn what this thing is.
So that way we still have type safety everywhere
in our code, even
if we don't know what something is right away.
So for example, when you fetch data from an API
you may not know what that data is
but by doing some simple type checks like this
you can figure out what that data shape looks
like and make sure it's what you expect it to be.