Transcript
00:00 Okay. Let's jump into our generic functions. We're gonna take, or we're gonna first create an identity function. So, literally all an identity function does is it accepts a value and then returns that same value. Like I said, seems kind of useless, just like this return value, or return type is useless.
00:21 But let's talk about what this looks like after we add our console logs right here. Boom. There we go. Hello and 42. Exactly what we expected.
00:28 So how does this work? Well, we've got, this, open angle bracket right after the name of the function, and then we have this type name. So this is a type named value. It's called a type parameter. And then you can use that anywhere you like.
00:46 Here in our case, we are using it, to define the type for this parameter value and then, we can use it for the return value. We can also say, let thing equal or be a value and that equals whatever and that's not actually going to work because, we don't know what the value is. The person who decides what the value is is the one who calls our function. That is what determines what that value type is going to be. So in the case of this call right here, you'll notice, it's saying the value of this type is the string quote hello, whereas in this case, it is the number 42.
01:23 So that, value type can literally be anything. And we'll have an exercise to show you how to constrain that to, be like a subset of possible values to make it a little bit more useful inside the body of the function. But, there are plenty of cases where, having a generic this simple is actually quite useful. So, that is our identity, function, and that is the basic syntax for an identity function. Now let's make our last function.
01:51 Yeah. Here we go. It's the function. There we go. Last.
01:56 Alright. So this is gonna take an array and we're gonna pass that, that, generic item to the array generic. So actually, this is what's called a generic interface or a generic type, and we'll be learning about those very soon. So, the point of this example was to show that you don't necessarily have to use the type verbatim for the argument. You can actually use it as a, an input to other generics and things like that.
02:28 So it's pretty interesting how that all works together. So then we're going to return our array, grabbing the array length minus one to grab that last one. And now if we get rid of these, we're gonna see three b and undefined. Let's look at that undefined one a little bit. This is kind of interesting.
02:46 So here, if we look at the, the type definition or the the, inferred type definition for last on this call, with the array one, two, three, we're going to see last number, array number, array, etcetera, etcetera. So it is able to determine based on what we're passing to it that our, item type is a number in this case. If we look at the strings a and b in that array, that, item type is going to be string because it determines that based off of the type of array that we pass it. To make that even more interesting, if we were to say, this is an array of numbers or booleans, now it's going to be able to just say, oh, okay. That's an array of numbers or booleans.
03:32 So it, is able to kinda follow through not just, what we pass here, but also what what does array do with this generic and how how does that how is that able to infer what that item should be? So pretty cool stuff. Now on this last one, we don't have, anything in that array. So, if we were to not specify anything, it's just an empty array, then we're going to see that the the item type is never because we're passing it, an array of nothing, which interestingly means that we will only ever get undefined because it knows that, an array of never is never going to return anything other than undefined when you grab an item from it. So because the item is never, the only array, or the only return result could possibly be undefined.
04:25 So that is interesting. Now if we wanted to be very specific about the type that we're giving it, then, we could say, no. This is gonna be an array of numbers And it's totally fine with that. We're still gonna end up with undefined, but it's able to say, okay. It's not array of never.
04:39 It's an array of numbers. It just happens to be empty. And that's fine. Now interestingly, if we, tried to do an array of numbers or, set the, the generic to number on this call, that's going to have a problem, with the array that we're passing because we're saying, hey. The array that I'm gonna pass to this is an array of numbers, but we have strings in there that's gonna be a problem for that.
05:02 So typically, I find myself, relying on the generic, for the or or, relying on inference for these generics. But sometimes you do need to specify that generic type. So, that is how you do that. Alright. Hope you had a good time with this one.
05:20 We'll see you in the next.
