If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
Here we have a utility function that creates a variable of type never. Now, as we have seen before, you are not allowed to assign anything other than a never to a never. This is because here we are saying that not allowed is something that should only accept something that should never happen. However, string is something that exists and will get assigned to this variable and therefore type three gives us a compile time error. Of course, you can still go ahead and assign a never to a never and that is perfectly fine. Now, what might catch you by surprise is the fact that you can also assign a never to anything else.
00:35
For example, here we have a variable of type string and we are assigning a never to it. And the reason why this is allowed is because the fact that this function never returns means that example will never ever get assigned anything other than a string. In fact, it won't even get assigned. The fact that you can assign a never to every other type means that it can be considered a part of every other type within TypeScript. So if you were to say that verbose is something that has a string or a never as far as TypeScript is concerned, it is the same
01:09
as you saying that verbose is something that has the type string. And in fact, if you hover over a verbs, you can see that this is exactly what TypeScript infers. Now this is extremely useful with conditional types and let's look at an example. But before we do that, we need to cover the concept of distribution of unions within conditional types. Here we have a conditional type, no empty that takes an input type T checks if it extends na undefined, and in that case maps it to never otherwise maps it back to the type T.
01:44
Let's pass in a union of two types, string and null into the nom empty utility type. When we pass in a union into the conditional type, it is essentially the same as the union of the conditional applied to the different members. In our case, the different members are string and now, so therefore we get a union of the condition applied to string along with the condition applied to. Now we will even go ahead and expand the two conditions in line to make it more explicit how TypeScript is actually viewing your code.
02:16
In the first case, string is not something that can be assigned to something that only accepts null and undefined and therefore it gets mapped to string. In the second case, null is something that can be assigned to something that only accepts null and undefined and therefore it gets mapped to never. So if we expand it further, what TypeScript is actually seeing is string or never. And as we have seen in our previous example, string or never, is just a verbal way of saying string. And indeed this is what is inferred for the example type
02:52
where we invoke the no empty utility type with string on. Now a good mental shorthand to have is that never in a conditional type is the standard way of removing members from any given union. Even though it is useful to understand how TypeScript is expanding your unions with formal rules, you don't have to think about it when you're actually writing code. When I see no empty string or now I just go step by step and think if tea is a string, it gets mapped to string. If tea is now, it gets mapped. Never.
03:25
So no empty string or no will be just string. I.