- Now following on from the exact same code
we have in the last video, there's another way
that you can kind of deal with this any unknown style
and it's a little bit of a middle ground between the two
because it still gives up on your type safety
a hundred percent, but it makes you so
at least the rest of your code doesn't suffer.
That's because we know if we use any, everywhere else
in your code that uses that variable suffers
'cause none of your code knows what it means.
While if you use unknown, you're essentially forcing
your code to learn what it means
but it requires a lot of actual upfront coding.
A lot of times if you're doing a fetch request for example
and you're getting back some data from that fetch request,
you may have a really complex object
and the code to check to make sure that's in the right shape
could be hundreds of lines and it's just not really feasible
to write that code all of the time.
So instead what you may do
is you may want to just cast the variable
to be a specific type.
Lemme show you what I'm talking about.
Let's just do a simple fetch.
We're gonna get our res data as JSON.
We'll come in here with a dot then.
I'm just gonna do a quick console.log of our data.
You'll see right here our data is a type of promise
that's returning anything.
That's 'cause I need to make sure I call this, there we go.
Now you can see that this is just a type of anything
'cause we don't know what this data is.
Well, if we're developing this API
and we're working with a backend team,
we know what this data should look like.
So, we can maybe get away with just telling TypeScript
what this is because we know what this is.
We just need to let TypeScript know what this is.
So if you have a variable and it's one type,
whatever type it is, whether it's any or something else,
and you want to specifically move it to another type,
that's where type casting comes in.
So I'm gonna take this code and what I wanna do
is I wanna cast this data variable as a specific type.
So, I can say I wanna cast it as,
let's just say it's going to be a to-do type.
Let's create that real quick.
Type, to do,
and we'll just give it a title, which is a name.
We're gonna make it super simple.
There we go, a string right there.
Perfect.
Now let's just return this.
We'll return our data as a to-do
and then we'll add another .then on here
which is gonna take in our to-do.
You'll notice now my type for my to-do here
is the to-do type.
So this as casting essentially is saying I expect
this variable to be a particular type,
which, in our case, is to-do.
TypeScript doesn't do any type checking.
It doesn't check to see if this is a valid to-do or not.
It just says, okay, you know more than me.
I'm going to manually set this thing as a to-do.
So this doesn't actually fix your typing problem
because if for some reason this data returned
is not a to-do, it returns like a person for example,
your code is still going to think it's a to-do
even though it's not.
So, it's not perfectly type checking you
like the unknown use case would've done.
But the rest of your code that's using this data,
in our case, this .then right here,
it now has the proper type that we expect this thing to be.
So, at least the rest of your code doesn't have a bunch
of anys flowing through it and it has the proper type.
Now in my opinion, this as casting is something
you should very rarely do inside of TypeScript.
And again, it's because it goes against TypeScript
and overrides TypeScript and you're telling it, you know
I know more than you so just trust me.
But it's very often that you can be wrong
and that your assumptions are not always
a hundred percent correct.
So I recommend using this as casting
as an absolute last resort.
I would use it before you use any
but I would definitely use it as last resort
and if it's the only way that you can make something work.
But I would recommend looking around
and seeing if there's other solutions.
Maybe you know, doing extra type guards
or something like that is going to be able
to give you the type safety you expect and this as casting
as to do, you can use it anywhere you want at all.
So we just have like a variable we can say
like const a equals whatever.
We could cast this however we want.
So we could say like, right now this is a string,
let's change it to a number and let's say that we wanted
to get the length of this as if it was a string.
You'll notice I get an error.
Well I can just come in here, I'm, you know, you know what?
Treat this as if it was a string
and now if I just add a semicolon right here so
I get everything to work, you'll notice I do get an error
'cause essentially I'm trying to convert a number
to a string.
But if I were to cast this to something like any,
I could say, you know what, this is an any type.
Now you can see that this is working as I expect it to
and it knows that A is a string.
So I can use this wherever I want to use it essentially.
And if I create a new variable,
like B is equal to A as string, you can now see
that this B variable is set to a string.
So it essentially just allows you to convert a type
from one type to another type but type script's gonna be
at least somewhat smart enough to tell you like,
"Hey it knows that A is a number
"and you can't convert a number to a string.
"So like this doesn't work."
But a lot of times when you're doing this type of casting,
it's 'cause you want to cast something like any
or unknown to another type.