- 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.
Another important difference between
void
andnever
is how they interact with other types.void
can be used as the return type for functions that returnundefined
, whilenever
can be used as the return type for functions that throw errors or have infinite loops. However,void
cannot be used as a subtype of other types, whilenever
can be used as a subtype of all types.