Okay, let's, let's start with the first thing, the conditional check. What are we going? I'm going to put this back so we can keep track of it. Um, what are we going to do in TypeScript types for a conditional check? So for example, we can make a type, not empty and it's going to take in, yeah, I'll call it normally everything in generic types is called T, but I'll call it S for string. Uh, for now you can also say here that it should extend string, uh, to make sure you can only pass a string into it. And, um, we're going to check if this string is empty. Well, we can just, we cannot do it like this, right? We cannot say true or false. We have to say that the string extends something. So in TypeScript types you can only do conditional checks by using extends. And that means that this string should extend this string or it should be assignable to that string. So if F is, if S is empty, well, that's not good. We'll make that never and never just means kind of, it's more nuanced than that, but this kind of means, yeah, this is a type of error. If, if, if something evaluates to an ever, you can only assign never to that. So that will in practice, all, all always be a type error. And if it is not extending the empty string, well then we have not empty. So then we just return S again. How do we use this then not empty off? Hello, for example, well, not empty of hello. That will just return. Hello, because this will not extend the empty string. So we will just return. What is the input if not empty off empty string that will be never. And then we know in our code, for example, Oh, uh, if, uh, yeah, that, that, that is an empty string. So this is the way we do conditional checks, uh, in TypeScript. We just use the ternary operator and the extends, uh, yeah, clause there.
Let's build another type to, to hammer this down. So we do a contains, for example, this is something we'll, we'll need to check if letters are contained in, in a string. So we have a string and a letter, and we want to check if this string extends, something that contains this letter somewhere. And if that's, I will just write it like this for now, if that is correct, then, okay. It does contain it. We'll return one, for example, and otherwise we'll return zero just if that's, we can also return true or false. Those are also types in TypeScript, so you can have a true type.
Comments