I have read this, but it is unclear what would be the difference between \'never\' and \'void\' type?
As Marius Schulz discusses in this article,
- A function that doesn't explicitly return a value implicitly returns the value
undefined
in JavaScript. Although we typically say that such a function "doesn't return anything", it returns. We usually ignore the return value in these cases. Such a function is inferred to have avoid
return type in TypeScript.- A function that has a
never
return type never returns. It doesn't returnundefined
, either. The function doesn't have a normal completion, which means it throws an error or never finishes running at all.
The return type of Promise.reject()
is Promise<never>
, meaning "it is never resolved".
So if a function returns Promise<never>
, I think it will return only errors. On the other hand, Promise<void>
might be resolved without value.