Now let's combine these two, map types and conditional types, and create a custom util type. So the util type that you want to create is similar to omit, but a little different. So omit takes in the key of, so this is what omit does. Type omit keys, omit person, comma, so it will take in all the person keys, let's say h, and it'll omit all these things and just return whatever the left, whatever the keys are left. Now what you want is, instead of giving the person, instead of giving the keys as a second argument, we wanna give the types that we wanna eliminate. So we don't wanna give the exact name of the key, but we wanna give the type of the key that you wanna eliminate. So we wanna eliminate here, Boolean and numbers, we don't want this, and we don't want this, we just want name and email. So once we give that it should return the string variables only. So how we do that? String keys only, so how we do that? So we just use the same signature that we have for omit, so omit takes in the main object as a base, and we also take in all the union types. And we use the same omit but with a different type, so basically omit expects keys as a second argument, right, that's what it expects, keys as second argument. But we are sending number, and we are sending that actual type as a second argument. So we want to convert this one to the number, we want to convert the type to the actual keys. So that's what is being done here. So we are using the map type to loop through the base object and get all the keys, and what I'm checking is checking the type. So this main object of key will return name or number based on what the key is, if it is name, then it's string. So it will be string extends the type to remove is number or Boolean, let's say string extends Boolean, it's not right, so it's not extend Boolean. Then we return the key itself, so we return name. So for name, it's a string, if it's extends, we return that, so in this example, let's say this is a string extends Boolean, it's not extends Boolean, so it'll be never, so that's what it's being here, and next is age. So age, a type of age is number, so this is number extends number or Boolean, yeah it extends, then it will return the number, the key itself, so it will be age. So I'm returning key, I'm not returning the type, I'm just returning the key directly. Again, email, the type of email is string, they will be never, then is student, so this one, type of is student is part of Boolean or number, yeah, then it'll return the type, basically is student, it'll return the key, basically is student, so this is what it returns after this is being executed. Again, now we want, we don't want this object. What we want is we want name and age to pass, right? So how we pass that? With this one, so if you remember this example we saw, this person can take in, this type, can take in keys of person, basically union of all the keys, right? So it'll return all the type of those. So if I pass in, let's say this is Boolean, right? It'll also return name, number and Boolean. What are all the type of these keys that'll be written. So in our example, it's a little bit different. It'll return the same thing, but what type we have? We have all the literal types, we have never, we have age as literal type, and never. And again, student is a literal type. So that's what being written. So this key of this is like this, and it will return all the actual types that's being given.
Comments