The last interesting type I want to show in this module is to never type.
Now we saw a function that returned white so that never returns anything.
Never is another type.
Functions can return.
And that might sound strange.
Let's have a look at how it works.
Let's say we have a function generate error or here I expect to get a message which is a string and
maybe some error code which might be a number.
Now inside of this function let's say we throw an error.
So this should essentially be a utility function that generates error objects and throws them.
So here we might throw an object which we can do in JavaScript we can throw any object or any value
as an error and that should have a message.
Property in which I stored the value of my message argument here and I want to have an error code property
let's say we're a store to code argument in.
So now it is would be a valid function and we can call this with an error occurred for example and a
code of 500.
If we do that and we compiled is file here we see that once this reloads we get our error.
Well as an error here and this might sound pretty abstract but actually it isn't having utility functions
like this would be pretty standard in bigger applications where you don't manually want to throw an
error in ten different places of your app but where you want to reach out to one convenient function
that builds the error object for you and maybe also froze it immediately so that you can call this function
with different inputs but you always have an error being thrown.
Now the interesting thing about this function is it does not just return wide.
I actually can specify that it returns wide because of course returns nothing but actually does not
just return nothing.
If we're totally honest this function returns never.
This function never produces a return value.
If I would try to restore the return value here and a console log result and I then compile my code
and this executes we see there is no undefined being locked here because since an error is thrown this
essentially crashes our script.
You could say it cancels our script and this will always be the case for this function.
Of course we can wrap it and try catch so that we can still continue in the script.
But this function essentially never produces a value.
This function always crashes the script or this part of the script.
If you're using try catch and therefore never returns anything and hence the return type of this function
actually is not just wide but also never.
Now the interesting thing is if you hover over it without assigning never you see the inferred type
as white all because never is a newer type.
It's been around for some time now but it wasn't built into the first versions of TypeScript and therefore
why it is typically assumed and it's not horrible to leave it at that but you can be very clear and
explicitly set.
Never asked the return type to make it really clear that this never returns anything.
So from a code quality perspective this might be clearer regarding your intentions and make it really
clear to average developers reading your code that this function is intended to never return anything
and to essentially crash or break your script or that part of the script and no function that would
never return by the way would be a function with an infinite loop.
So if we have while true in there that creates an infinite loop and that therefore all would be a function
that never returns the error function here or the function that generates and throws an error probably
is the more common use case though so never.
Also an interesting type which you can use on functions for cases like this one here.