Transcript
00:00 Alright. Let's get right into it. So we've got this, new user and we're going to get the return type of create user. So we're gonna say type of create user. That's gonna get us the type of this function.
00:11 And if we were to store that somewhere, so, create user type, then we'd say type of create user and you can hover over that and it takes all the parameters, the return value, and, converts it into a type. And then from that, we pass it to return type and that gets us the return, value. So effectively, just this. So if we hover over here, we're gonna see that exactly. And so that is a quick and easy way to get the return type of a function That could be really useful if you're trying to recreate or or make a a wrapper function around it or, have some sort of type that says, yeah, pass me this argument and it will have to be the exact same type as this thing or I wanna pick the pieces and then I'll override some options, whatever.
00:56 There's a lot of, possibilities with return type. And then we have the parameters. So if, especially, yeah, if you're trying to make a wrapper that, like, simulates the same API as the function you're calling or something, then you can use parameters to help with that. And this is going to give us, a, it's effectively an array, type, but it's a constant array with name as string, email string, and age of number. So these are the parameters for create user.
01:24 So that can be quite useful. And then, we have our fetch user, right here that has our return type and, it's an async function. So here, if we wanted to get the return type, then we're gonna get a promise. Let's see if it'll show me Yeah. It's not If Let's break this out.
01:41 Break this down to, yeah. Fetch user type type, fetch user return and then we can do Yeah. Await it. Alright. So we can look at each one of these pieces.
01:54 So here's the the return or the the type for the fetch user function and then the return type for the fetch user function is gonna be this promise. And so if I want this piece just by itself for any reason, then I can put awaited on that and now I'm left with just what's left. So what would happen if I awaited this and it was resolved? That is what awaited does. Before awaited well, waited is relatively new compared to lots of these.
02:21 And before awaited showed up, I had to make my own version of of this utility type, and it was a bit of a pain. Really happy that this got added. It's it's quite useful for, using inference based on the return value of some async function. Super awesome. Whoops.
02:37 Okay. There we go. And now we're gonna make our, here, we'll just let it all complete. Process data args. This is gonna be the parameters of process data which accepts, data as array of strings and options which has a limit of a number and then it returns a number.
02:55 So, as you might expect, we're gonna have data as an array of strings and options with the limit number. K? Perfect. And now we can create that wrapper function I've been talking about. So this is going to do a create user, but then, we're going to wrap this function as a logged, create user.
03:15 So before create user is called, we'll add a log. So if you want to, I don't know, log every time our user is created, you call this one instead. But the cool thing about this is that logged create user can kind of act as if it is a create user. So what you could do, let's say that you have create user, we'll export that and then one day you're like, no, I wanna log every single time, but I don't wanna change this or maybe you import this, this function from a library or something. You can wrap it and you don't have to worry about if any of the parameters change because you're deriving what your parameters are based on the, the type signature of that function in the first place.
03:58 And so you can make your own little wrapper and then export that or you could say export that as create user. Whoop. Create. My goodness. Create user.
04:10 There we go. And so now anybody importing it is actually gonna be calling logged create user without knowing it, because it's just called create user. So that's powerful. And so we we take these args, all the arguments that they're gonna pass, we don't know what they are. We don't really care.
04:25 It's all derived from these types, and it's gonna return a new user, which is the return type of create user, and, then we can wrap that. And, honestly, lots of the times the way I would write this is, parameters type of create user, and then I just wouldn't have a return type because that's gonna be inferred from what we're doing inside the function. And so then I don't even have to have names for any of my, types, which is great because naming things is a big pain. So, that's it. Yeah.
04:54 Good job on this one. Hopefully, that was interesting, exciting, lots of cool utility types for your functions.
