If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
Now because TypeScript does quote flow analysis, there are certain instances where it infers that something can never occur. For example, here's a function that always throws and therefore it can never return a value and TypeScript infers its return type to be the special type never. Another example of a function that never returns is something that has a very obvious infinite loop. For example, while two here, once more TypeScript infers that the return value of this function is never as dysfunction, will never return. Now never can also be used
00:33
as an explicit type annotation in TypeScript. For example. Here we have a variable which we have annotated to be never a key feature of The never type in type script is that only something of type never can be assigned to something of type never. So if you try to assign a number to a variable of type, never type script will give us a compiled time error. For example, number cannot be assigned to something that is never. We can use this fact to ensure that all important cases are handled in our code base. Let's look at an example here. We have a type square with a member size of type number
01:07
and then we have a type rectangle with member width and heights of type number. Then we have a union type shape that allows us to work with square and rectangles in our utility functions. Now our objective is to create a utility function that returns the area for any given input shape to ensure that we handle all the types of shapes that might be input. The first thing that we do is create a never variable ensure all cases are handled and try to assign the input shape to this variable. Now because the input parameter is of type shape, which is not assignable to something of type never,
01:41
this will result in a compiled time error. Only after we've handled all the cases will as truly become never. So this error is going to force us to handle the different cases and the first case that we handle is that for the square. Now once this case is there, the error changes. That rectangle is not assignable to Neva. So the next step is to handle the case for rectangle and now the error goes away indicating that we have successfully handled all the cases. Now it is a good idea to leave this never assertion as a part of the area function, ensuring code correctness
02:16
for future modifications. For example, if in the future we decide to support the circle type as a part of our shape, we immediately get a compile time error because circle is not handled within the area function and the error message is that circle cannot be assigned to never. This helps the person making the modification and they can ensure that this new type is handled in all the utility functions. Now one final thing worth mentioning is that all parts of a function must return a value, otherwise TypeScript infers that the return type of the function might be undefined.
02:50
For this particular case, we can simply return the insure variable and now type strip correctly in fill that the return type of the function will be a number.