#18 Never Type in TypeScript | TypeScript Basics & Data Types | A Complete TypeScript Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the last lecture we learned about Union type in typescript now in this lecture let's talk about never type so here we have a function called greet user and this greet user function it is not returning anything so by default its return type will be void as you can see this we already know from our previous lectures now we also have another type called Never and when we use never type that means that function is not going to return anything it is not going to return anything ever so for example if I set the return type of this one as never you see we have an error if I over over this error it says a function returning never cannot have a reachable end points now what does this mean let's understand this with an example so I will set this type as void and now I'm going to create another function and let's call this function function may be create error you can name this function anything and what this function is going to do is it is going to take two parameters the first parameter will be error message which is going to be of type string and the second parameter will be the error code and this is going to be of type number and all this function is going to do is it is going to throw a new error so what we are going to do is we are going to use this throw keyword you might already know about this throw keyword we use throw keyword to throw something from a function and what do we want to throw here we want to throw an object in that object let's say we have a message property we want to set this message property to error message and let's name this as error message and then in that object we also let's say we have a code property and this code we want to set to error code so from the this function we are throwing something okay from here we are not returning we are throwing something and after this throw statement if we have any code for example console.log error message this code will never be reached because from here we are going to throw this object and the function will immediately exit so from here nothing is getting returned from this create error function nothing is getting returned so that's why the type of this create error as you can see here it is set as void because we have learned about the void type when we set the return type of a function as void that means it is not returning anything so by default it will be void but what we can also do is we can set its type as never which simply means that this function is never going to return any value let me remove this line now let's go ahead and let's call this function and here let's pass an error message maybe internal server error and let's specify the error code 500 okay so we are passing this message for this error message parameter and this 500 for this error code parameter let's save the changes here let's compile this app. TS file so everything is compiled successfully let's reload the page here and you will see at this line it is logging hello John which is okay and when we are calling this create error function here we have an uncut error logged and this is not an error actually from this function we have thrown this object so when we use throw statement it is thrown as an error so so that's why you will see an error logged here okay and in that error what we have thrown we have thrown this object where we have a message property and an error code so you see here also we have a message property internal server error and the error code which is 500 so we use this never type when our function is never going to return any value from this function we are not returning any value instead we are throwing an object we can say we are throwing an error object and this is a common use case we do in our day-to-day programming we create a utility function which can create and throw an error and we simply call that function from our code whenever we want to throw an error and this function is never returning any value so we can also set its return type as void because using void also we specify that function is not returning any value but when we set the return type as never this never is different from void now how it is different let's see that so this function here this create user its return type is void and this function create error it's return type is never so when we log the result of a void and a never let's see what happens so here I'm going to use console.log statement and there I am going to call this GRE user function so we have learned earlier that if a function returns void and if we log its result it is going to log undefined in the console here let me pass the value for this name parameter okay so here we are calling this greet user whose return type is vo now let me copy this line and let me paste it here and let's also call this create error function and the return type of create error function it is never and here let's specify another error message maybe page not found and let's say error code code is 404 so just notice what happens when we are logging the result of greet user whose return type is void and when we are logging the result of create error whose return type is never so let's save the changes here let's compile this app. TS file now here we don't see anything logged because at this line an error has occurred so after that nothing will be compiled so let me comment this code let me also comment this call okay so that we will only have logs from these two lines let's save the changes and let's compile this appts again and you see when we are logging this result of greet user it has logged undefined okay and when we are logging the result of create error first of all this create error will be called and it will throw this error so we are seeing that error here that uncut error and then when we are logging the result of calling this create error function there we don't see any value we not seeing even undefined when the return type is void at that time when we log the result of the function it logs undefined but in case when we call a function whose return type is never and if we try to log its result it logs nothing so that's why we say that this function the return type of this function is never because it is never going to return any value in case of white it is at least returning undefined but in case of never it is never going to return any value so that is the difference between void and never type so when we say never it means a function is never going to return a value not even undefined so we can explicitly set the return type of a function as never if we never want to return any value from that function if we do not specify the never type explicitly like we are doing here in that case by default the return type of that function will be inferred as void so if I remove this never from here the return type of this create error will be inferred as void so we need to explicitly specify that the return type of this function should be never another example of a function which will never return a value is where we have an infinite Loop so for example let's say we have a function let's call this function maybe infinite you can name this function anything and inside this function let's say we have a loop maybe while loop and the condition for this while loop is let's say always true in that case this while loop it is going to create an infinite Loop it will never return because this condition will always true so this Loop will keep on iterating infinitely so in this case also the return type should be never because this function fun is never going to return so this is another example of never type all right so I hope with these examples now you know what is never type and when should we use never type we basically use never type to specify the return type of a function which is never going to return this is all from this lecture if you have any questions then feel free to ask it thank you for listening and have a great day
Info
Channel: procademy
Views: 570
Rating: undefined out of 5
Keywords: typescript, data types, configuration, classes, objects, ES6 features, decorators, tupescript tutorial, complete typescript course
Id: k1LW7VjTF1U
Channel Id: undefined
Length: 9min 36sec (576 seconds)
Published: Thu Apr 18 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.