I am messing with the Typescript playground, and the only differences I'm finding between void
and undefined
are as follows:
I can assign an
undefined
value to avoid
-type variable, but I can't do the opposite.A function whose return type is explicitly stated to be one (and only one) of these two types will trigger errors or fail to trigger errors in accordance with the rule stated above - except when there is no return statement at all, in which case the
void
return value type is still acceptable, while theundefined
return value type is not.
I don't know of any other differences. Are there any other differences? It appears to me that, as long as the relevant strict-mode compilation flags are on, the type undefined
encompasses exactly one value - the JavaScript value undefined
- while the type void
encompasses exactly two conceptual values: the JavaScript value undefined
, and the variant of the JavaScript value undefined
that arises when a function returns by reaching its end rather than by reaching a return statement or by throwing. (And of course, these two variants of the value undefined
are exactly the same thing as far as ordinary JavaScript is concerned.)
Am I missing anything?
(I guess that if some option is changed then null
will also get involved here, but I'm not as interested in that, personally, so I haven't looked into it.)