Instructor: Generics have always been a good solution to making reusable types. You can pass in one type and return a completely new type from it. It's like a function, really. TypeScript's new conditional types take this to the next level.
Instead of simply copying the value of whatever's passed in here to some location in the generated type, we can tell the compiler to intelligently analyze a ternary expression.
If my generic parameter extends a string or is a string, then my container property will be of type string container. Otherwise, it's going be a number container. I've created in this, that's of an item down here. I'm passing in string as the generic parameter.
Let's try this. If I try to access the container on it, we can see we get format, split, all of which are string container property. Now, let's try to pass in a number here, see what happens. As expected, we get number container properties now.
I can also build an array filter type. All we have to say is that, if the generic parameter extends an array, then we just return that type. Otherwise, we shall return the never type, so the compiler can ignore it. Let's try this out.
I'll create a new type called strings or numbers. I'll use the array filter type that I created. I'll pass in a type that can be either string or a number, or an array of strings, or an array of numbers.
Now, if I hover over my new type, I can see that it's either the array of strings or the array of numbers. Anything that wasn't of type array has been filtered out. All of this works because of two mechanisms.
Conditional types distribute over each element in the set of possible types that we pass into it. In my case, it's going to apply this ternary operation to each of these four types individually and replace each one of them with whatever this expression returns.
In my case, it's going to return never for the first type. It's going to return never for the second type. It's going to return array of string and it's going to return array of numbers for the last type.
Second, because by definition the never type can never happen, if TypeScript sees it in a union of types like we have here, it's just going to be ignored. What we end up in the end is a type that's either an array of string or array of numbers.
Finally, conditional types can become a replacement for function overloads. I have here a type item service, which has a function getItem. This function is a bit versatile, because if the ID is a string, then it's going to return a book.
That's because books are indexed by strings. If it's a number, it's going to return a TV. That's because TVs are indexed by numbers. Now, I can try to overload this function with a few definitions, and it works.
Let's see if we can make use of conditional types and keep just the single definition. I'll just add in here. If key is a string, then return a book, otherwise, return a TV.
I'll pretend this item service variable here exists, and it's instantiated. I'll attempt to get the book from it by passing in a string. Then, I'll do the same thing for the TV by passing in the number.
If I hover over the book, I can see TypeScript correctly infer that it's a book. If I hover over the TV variable, TypeScript correctly inferred that it's a type TV. One problem with this approach is that I can pass in a Boolean in here. It's still going to work and TypeScript will think that this is still a TV.
To stop that from happening, I can just lock in the possible types of the ID that can be sent in here to just strings and numbers. The moment I do that, TypeScript will start complaining here that I'm passing in something that's not really allowed.