Loading
Current section: 6. Conditional Types 4 exercises
solution

Basic Conditionals

Transcript

00:00 Alright. Did you have a good time? Let's go through it together. So we're gonna make a is string type right here. This is going to accept a type.

00:07 It's a generic and if that type extends string. So if it matches the type string, then our type is going to be assigned to true or or rather this type evaluation will evaluate to true. Otherwise, it will evaluate to false, and then that gets assigned to our is string type. So if we uncomment, whoops. There we go.

00:26 Uncomment both of these lines, you pass string or you pass number, you're gonna get true and false as you might expect. Let's do is array next. So here oh, spoiler alert. We're gonna just say any. We'll we'll get to that in a bit.

00:39 So same sort of thing, if it extends an array, and it can have any value in that array, that's fine. That's gonna be true or false. And here, if we bring back our is array, it's is array array of strings. That's gonna give us true. If we're just passing it a string, then that gives us false.

00:58 And this can, be anything. So here's Boolean, still gonna be true. So we didn't explicitly say what type of array we care about. You could say, like an array or u, t u v c. I don't like these single file name, but the agent just keeps on doing single letters.

01:15 And so what am I gonna do? So here we could say, u would be the, array type, the type in question. Okay. Maybe I know why they do single letters for this stuff. So we'll do u and then pass u right here.

01:32 And so now it's this this is going to require a second argument for this generic. And we say, is this an array of booleans? And then is this an array of strings? And in this case, no, it's not. It's not an array at all.

01:48 And then you the AI kinda helped with this. You could say default this to any and now you don't have to provide this and we've just made our is array more powerful because we can be explicit and say, it can be an array of anything or is this an array of numbers? And that is going to evaluate to false. And then you can use that for other things. So this could be part of another expression and ternary thing.

02:12 It's it's wild how deep this stuff can go. Okay. Let's continue. Let's make an is function. So if the incoming type is, a function, then we'll get true or false.

02:22 Okay. Great. And you can, imagine what that would be. Just make a function type of that or, yeah. Type of that function and boom.

02:30 Bob's your uncle. Okay. So flatten is where things get kind of interesting here. So if t is an array, then we're gonna return the element type. Otherwise, we'll return the type that's given to us.

02:41 So in this case, oh, That's funny. We'll we'll talk about infer instead here in just a yeah. Here. It's trying to auto complete infer infer. But let's get we'll get to that in a second.

02:50 So we're gonna take the type. If that type is an array of the given type or an array of anything, then we're going to, pass u. Now that's interesting. Let's let's actually let's try this out. So, u or we're gonna pass the t.

03:05 So here, this is an array, so we're gonna get back any. But, no, what I want is to get back string. Right? That's that's what we're trying to or, flatten or unwrap this type. We want to get what the, the value of the array is, if it's there.

03:21 T six is fine because it's a number. This doesn't end up being an array, so we just end up in, the alternate and we're in a good spot. Now the agent is helping us fix this, so let's hit tab. We no longer are going to accept a second argument and instead, oh oh, back it up, back it up, back it up. Let's just try that second argument just for the fun of it.

03:40 So string and here, now we're gonna get string. If I put boolean, then we're not going to get that. We're gonna get back that string array. So I don't wanna have to put that. So how do we avoid it while still not having to do this anything?

03:53 We are going to infer u. And so whatever, it's an array of something, I don't know what it is, but I'm gonna assign that thing to this u type, and then I'm gonna return that, from this expression. I'm not actually returning it. They're just gonna that's the result of this expression, for the type, and that solves our problem. So infer is pretty powerful.

04:17 It's pretty cool. You can feel free to, practice this and and throw this in a couple different functions and everything if if, you would like to. But that is the basics of, these conditional types. Pretty cool stuff. Extends is pretty wild.