Transcript
00:00 Alright. Let's jump into this. So we've got a, type guard is string array. We're going to create that function is string array. And right here, we have a value.
00:11 Its value is unknown. You might have had your agent say any, or maybe you just left it off entirely. That's fine. We're gonna talk about any and unknown, really soon. So you can, worry about that later.
00:25 We'll we'll get to that and talk about the distinction there. The important part for this exercise step is what comes after. So, this is technically a return value, but we're, we are narrowing the default return value, which by default would just say, this returns an array or or returns a, a boolean. In our case, we're gonna be very specific and say its value is an array. You know what's kinda interesting?
00:53 This what we are returning is a boolean, but this is actually, interpreting that correctly. It's saying value is an array of strings. So this that inference is actually really good. Lots of times it's not gonna be quite that good and so I'm going to be explicit here and say, what we are returning is that the fact that value is an array of strings. That's the syntax for a type guard.
01:19 So the thing that you pass and you could pass multiple things, so you could say other thing. But what you are returning is that, this value that was passed is this type. That's what we're trying to validate with this type guard function. So it's a special kind of function that says something that you gave me is of this type. And then we say, the value is an array and every item in that array is a string type.
01:49 Okay. So with that then, we can, come down here and update this to use our type guard. So instead of is array, we can actually say is string array. And now we know that this input is an array of strings. And same thing down here, we can make a couple type guards.
02:05 So yep. Thank you AI for being so helpful. So we've got our is admin user. So value is unknown. Value is admin user.
02:14 Value is regular user, etcetera, etcetera. And in each one of these, we are still using the, in, operator, that in syntax that we we were before. But what this does for us is it makes it so down here, everything is a little bit easier to, to understand and and to read. If is admin user, then the user has permissions. If is a regular, then the user has a subscription, etcetera, etcetera.
02:43 And that's really nice because then we can encode the logic around what type a certain value is, inside of these type guards. And, then the code that's using those just reads really nicely. And we can also keep it all in one place. That's one of the benefits of functions in the first place. So, let's go ahead and export all of those.
03:03 You could feel free to to throw console logs in here and stuff to verify that everything is still working, but it is. I promise. So good job on this one. Now you know all about type guards. So great work.
