- So to get started talking about the never type
I'm actually gonna be using the exact code
from the last video
because it ties in perfectly
with what the never type is meant to be used for
and the never type in TypeScript,
it's just a type that means
that it is never possible for it to be anything.
Essentially, the never type is saying
that you should never be able
to get to this point in the code
and this variable should never even exist
'cause it never has any type to it at all.
The perfect example is a switch statement
where you have essentially three values here.
It could be any number of values,
but we have three values
and we're accounting for all those different values.
So when I come into here
and I try to create a default case
and I try to counsel log,
just like I've been doing before,
what my priority is
you'll notice here that the priority has a type of never.
And the reason for that
is because we've checked for high, normal and low
which are the only three options that our type can be.
So we know if we've checked for all three
of these different types,
that it is impossible for it to be anything else.
So it's impossible for it
to reach this default value section.
This type may seem useless to you,
other than the fact that,
"Hey, I know that this can never be anything",
so when I try to do something on it,
you'll notice I get no auto complete, no nothing
and I'm always gonna get an error
because the never type cannot be used
with anything, obviously.
This is actually slightly more useful
than you may think.
One thing that you can do with the never type
is you can actually check to see
if something is the never type
to know if you are actually checking
all your possible options.
What we can do down here
is we can just essentially create a variable
to determine if we're exhaustively checking this.
It doesn't matter what you call this
we'll call this exhaustive check,
just like that.
And we're gonna say that the type of this is never
and then we're gonna set that equal
to our todo.priority.
Essentially, we're just gonna set this
to whatever the thing up here is
that we're switching over.
And then we can do whatever we want.
It really doesn't matter.
We can just return it or do whatever.
It should never get to this point.
So it doesn't really matter.
And let me just make sure I spell this correctly,
so it's a little bit better.
There we go.
So it should never ever get to this point.
And right now all of our code is working fine
but what happens if we add a brand new priority?
Let's come in here and we'll say like super low priority.
Now when I save and come down here
you're gonna notice I get an error
and it's essentially saying the type
of string is not assignable to the type of never.
And that's 'cause this priority here
could still be super low.
We've checked for high, normal and low
but we haven't checked for super low.
This is gonna throw a TypeScript error for me
letting me know,
"Hey, you haven't checked for this thing yet."
This is one use case where the never type is really useful.
And what I can do is add in my case for super low,
break out of it, do whatever we want.
And now you can see that this is working again.
So if you want to create a switch statement
that requires you to check for every single possible option
adding in some simple code
like this where you just have a variable
it doesn't matter what the name is,
I'll just change it to A for simplicity,
that you manually set to the type of never
and you assign it to whatever
the thing inside of your switch statement is
what you're checking.
That's gonna make sure you always check
for every single possibility,
which is really useful for union types like this.