- A function that doesn't explicitly return a value implicitly returns the value
undefinedin 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 avoidreturn type in TypeScript.- A function that has a
neverreturn 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.
Another important difference between
voidandneveris how they interact with other types.voidcan be used as the return type for functions that returnundefined, whilenevercan be used as the return type for functions that throw errors or have infinite loops. However,voidcannot be used as a subtype of other types, whilenevercan be used as a subtype of all types.
TODO: Add undefined vs void here