on-internet
← Community

why should we use "never" when we can use void while throwing Error?

Stack Overflow ·

I’m learning TypeScript and I noticed that when a function always throws an error, we can define its return type as never: function throwError(message: string): never { throw new Error(message); } However, this also works: function throwError(message: string): void { throw new Error(message); } Since both implementations throw an error and never return normally, I’m confused about why never is recommended over void in such cases. My understanding so far: void means the function does not return a value. never means the function never completes normally (e.g., throws or runs infinitely). Questio

Comments

Sign in or sign up to comment and upvote.

No comments yet.