Transcript
00:00 Alright. Let's jump into types. You'll notice that right here we do not have a, type mentioned. It would normally would say a number right here. Right?
00:08 What we've done so far. You can actually remove that and TypeScript knows just by looking at what the function is doing or specifically looking at what is being returned that the return value is a number. And I don't think that I've mentioned this yet, but I I probably should. You don't have to return anything from a function. When you don't, then the return type is called void.
00:28 So it's kind of like null and undefined sort of except it's, it basically is is nothing. We're not expecting anything to come back, and so you can't do anything to it. So for example, if I said multiply, four and five, and then I said to string, woah, to string on it, we're gonna get, an error here because, it is void. If I return undefined, we actually, get the error in a different place because the return value here is undefined. You can't call anything on undefined.
01:00 So void and undefined, not exactly the same thing, but basically. And by actually returning, then it knows, oh, that's a number. Numbers have a two string method so I can call that. Okay. Great.
01:12 So that is, that is cool. And most of my functions, I actually do not give a return type and I just rely on whatever is returned. Sometimes it can be useful, and, and it helps TypeScript to actually put errors in good places. But, yeah, lots of time, I just find that it's, irritating to have to, oh, I updated the return. Now I have to go up to the, you know, return value or return type and update that.
01:36 And, yeah, it's it's fine either way. I'm not gonna, yell at you if you add a return type, but I just typically don't do that myself. Okay. Now, let's look at divide. Now, that one's interesting because you can't divide by zero so we're gonna say return this value here.
01:54 But what that does is it says, well, you can return a number or you can divide by or you can return that string. And that's not what we want for our divide. So I'm gonna say, this returns a number for sure and then that's going to help me in my function to make sure that I am actually returning what I said I was gonna return. This is one area where actually having a return type can be really helpful and I just wanted to show that to you. So you, if you really want your divide to avoid this problem, then instead you can throw an error.
02:23 We haven't talked about throwing errors yet. That is a later thing but this is how you handle like situations where that should never be the case. If you're trying to call divide with zero, that's your problem. I can't do anything about it so I'm gonna throw that error over to you and you're gonna have to handle it yourself. Okay.
02:42 And then we're gonna create a new function called is even. This is using a fun operator called the modulo operator or modulus which is basically take any number, divide it by the number on So take this number on the left, divide it by the number on the right, and then give me the remainder. That's what the modulo operator does. And so, yeah, that's how you can determine if something is even is that number modulus two, whether that is zero, is gonna tell you whether it's even. And in our case, we can let TypeScript infer that return type and it will correctly infer it to be a boolean.
03:20 And I can uncomment that and there we go. We got our 20. Four times five is 20. 10 divided by two is five. Let's see what happens actually if I say 10 divided by zero.
03:31 Kablooey failed to load the module because we're trying to evaluate this module and it says cannot divide by zero. And cannot divide by zero. I'm very excited about this. Okay. Sweet.
03:42 So then we can, say, is even true and false, ta da, and everything's working perfectly. The point here being you can rely on TypeScript's type inference for return values. I typically do, almost all the time. But for arguments, for those inputs, you typically need to type those yourself. If you don't type them, then they will result in being the any type which you don't want.
04:06 Even if the typescript can look at this and be like, well, that's being used as if it's a number. It's not going to do that. It's gonna be a little bit more defensive there and so, you wanna do that. And the nice thing about that too is if you pass a string here, then it's gonna be like, yeah, you passed a string. You probably shouldn't do that.
04:24 And so, it is nice to have those inputs typed. But again, the return types don't have to be typed and that's nice.
