Transcript
00:00 Okay. So never types, typically show up when we're throwing errors. You can also use them for some pretty advanced typescripty things. But, yeah. Pretty much when we're, throwing errors and stuff.
00:12 So we're gonna create a throw error function, and Marty the money bag gave this to us because, it would just be a waste of time to have you type it all out. I I mean, I guess you could type it all out. It's good reps and stuff. But, yeah. Here's what a function that is typed to return never looks like.
00:30 So it's literally just a function that throws an error and that's that's it. So the reason that it returns never is because it just never returns. There's nothing that it there's no situation where, calling throw error, would result in a value because before it gets to here, the error has been thrown. So it's gotta evaluate this side first to determine what this value variable should, be assigned to. But because we're throwing an error, once it hits this line of code, it stops execution.
01:02 It's done. That's what throwing errors does is it's just we're done executing. We're not going to evaluate this side of the equal sign because we stop execution once we hit a throw. Now, we can do try catches and stuff we'll talk about later, but, yeah. Throw, throwing like this, just stops all execution and that's why value is never And the value of this, is that, if I tried to use it, like value, yeah, too fixed or whatever, I'm gonna get a typing error here because it's saying this could never be.
01:33 It's impossible for you to actually have a value in the value variable. So that is, that's basically never. And here, would you look at that, TypeScript is, telling us void. So it is sometimes useful to explicitly type your, functions as having returned never. Now in our case, it's it's fine because, we're going to end up with, too fixed here anyway.
01:58 If I try to return, hi or whatever, that is going to be all grayed out and our return value is still actually going to be a string. So you might argue that, TypeScript could be improved here because it should know, like, you're throwing an error. You're never gonna get to that return value, so that return value should be string. So this is where, like, explicitly typing it as never can be useful. But, yeah.
02:22 I just I don't really run into that, all that often. Sometimes, you could say or string right here and then, you can return a string. You still get the grayed out thing right here in your editor. You typically wouldn't do this. You'd say, if this, then throw an error.
02:38 Otherwise, do this thing. And in that case, it, will the types will flow a lot better. But, anyway, that is, the, never type. Let's continue from here just to get a couple more reps and and look at a couple other things. So here, in this case, we've got our parse number.
02:58 We have our value string. We're gonna return a number, and we're going to determine if the number is not a number. So if this, value because not all strings can be turned into numbers. Like if I were to put, whatever, that's not a number. Right?
03:14 So we're past the value into the number, constructor, here and that gives us the number primitive. And then if that if the number constructor here says that this is not actually a number, then we're gonna throw an error. Hey. The value you gave me is not a valid number. Otherwise, we'll return the result.
03:35 And here we're typing it as a number return, so that's what we're gonna get back. So just interesting, even though you're throwing doesn't necessarily mean that your return value is going to be, never. K. Great. You and so you could actually end up in a situation where you say const, one equals parse number one.
03:56 And then you say one to fixed and TypeScript is totally fine with that. But the reason that that's fine in this case is because, once let's, yeah, TypeScript doesn't know that the string one is not going to evaluate to a number. But it does know that if it doesn't, it's gonna be throwing this error, here. So we're not even gonna get to, continue the rest of the program if, this doesn't evaluate to be a number anyway. And so we don't need to worry about this because this does actually also return a number and in that case, one will be a number and we'll be happy.
04:34 So this is a happy case and the rest of the program will evaluate correctly. This is the sad case, but but the rest of the program won't even evaluate, correctly anyway, so we don't need to worry about checking the rest of this. It can just be a number. Okay. Great.
04:49 And then we have our ensure positive function. So let's, just some more reps with this. Same sort of idea there. Let's add our, test here, right there. And we're gonna get invalid numbers, so let's fix this one.
05:05 Number must be positive. Let's make that. And there we go. Tada. Everything is working as expected.
05:12 Okay. So hopefully, now you have a little bit of an understanding of the never type if you run into it in, the code that your AI is generating or that you need to write yourself, or that you see in open source and elsewhere. That is what never is. It's just a situation that could never possibly happen. Good job.
