Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between never and void in typescript?

Tags:

typescript

I have read this, but it is unclear what would be the difference between 'never' and 'void' type?

like image 905
ironic Avatar asked Jun 19 '16 18:06

ironic


People also ask

What does never mean in TypeScript?

TypeScript introduced a new type never , which indicates the values that will never occur. The never type is used when you are sure that something is never going to occur. For example, you write a function which will not return to its end point or always throws an exception.

What is difference between void and undefined in TypeScript?

Again, void is not an alias for undefined and an expression of type void may have any value, not just undefined. Show activity on this post. void is not the same as any , nor the same as undefined .

Why void is used in TypeScript?

TypeScript Data Type - Void Similar to languages like Java, void is used where there is no data. For example, if a function does not return any value then you can specify void as return type. There is no meaning to assign void to a variable, as only null or undefined is assignable to void.

What does never [] mean?

Definition of never 1 : not ever : at no time I never met her. 2 : not in any degree : not under any condition never the wiser for his experience.


1 Answers

In imperative languages, void can be thought of as a type containing a single value. Such languages do not provide a means to construct or consume this value, but a void function can be thought of as returning this trivial value.

In contrast never is a type containing no values, which means that a function with this return type can never return normally at all. This means either throwing an exception or failing to terminate.

like image 143
Lee Avatar answered Sep 20 '22 10:09

Lee