I have read this, but it is unclear what would be the difference between 'never' and 'void' type?
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.
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 .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With