`void` is not a unit type in TypeScript
— TypeScript — 2 min read
In type theory a unit type is any type that represents exactly one possible
The unit types in TypeScript include
undefined, and literal types.
TypeScript also has the type
void which is used as the return type for
functions that don't have an explicit return value.
so at first glance it would seem that
void is an alias for the
But it is not!
We can test this by checking assignability:
If they were the same type,
void would be mutually assignable.
According to the Handbook,
void is] the absence of having any type at all."
TypeScript is nicely consistent with the behavior of types as sets.
any is an exception because it does not behave like a well-defined set.)
"The absence of any type at all" is not a sensical statement about sets;
so that description seems odd to me.
But that may be a way of explaining that in some ways
not behave as a well-defined set.
undefined is assignable to
void which tells us that the type
include the value
From a type theory perspective the fact that the reverse is not allowed implies
void represents a set of possible values that includes values other than
And we can see that is true in practice.
If you have a variable with a function type where the return type is
function is assignable to that variable regardless of the actual return type as
long as the argument types are compatible.
This behavior is convenient - instead of requiring certain return values the use
void in this case is more like a statement by the caller that it will
ignore the return value of the callback.
But we can see in this example that although the type of
values assigned to
x could strings, numbers, or booleans.
x could be assigned any type of value!
This makes it look like it would be most accurate to think of
void as an alias
That would be consistent with the assignability tests from before:
undefined is assignable to
unknown is not assignable to
But now let's look at the ways in which
void does not behave like a set.
void were a set that contains every possible value
(and in the
foo example we have seen that in practice it is)
then we should be able to assign any value to a variable of type
But it turns out that the only value that TypeScript will permit assigning to
a variable of type
It looks like either TypeScript has a special rule that prohibits assignment to
void variables, or TypeScript thinks of
void as a small set but the
return callback feature lets non-
void values "leak" into
My suggestion for making TypeScript more consistent is to make
void a proper
It's possible the reason that it was not set up that way in the first place is
In any case, that is how I am going to think of
void going forward.