Member-only story
What Is Void in TypeScript?
A brief explanation to tell void apart from undefined or null

While looking at TypeScript code, you may have come across the void
return type.
It’s most commonly seen as a return type to functions. I don’t see it used very often, but I’ve found it to be a helpful addition to my own coding style.
According to the TypeScript docs:
void
represents the return value of functions which don’t return a value.
Whenever you see a function returning void
, you are explicitly told there is no return value. All functions with no return value have an inferred return type of void
. This should not be confused with a function returning undefined
or null
.
Even though functions infer void
as their return type when nothing is provided, I still force myself to add it. I want as much of the code I write to be deliberate. By providing the void
type to my functions, I am telling my future self or whoever reads my code that I meant to return nothing from a function. Seeing a void
return type on a function has helped me out when debugging as well.
If I am looking for a strange return value from a function, I know it’s not coming from a function returning void
. I always indicate when I am returning something, why not do the same when I return nothing.
Below are a few places where I find the void
return type useful.
Generic Return Type
If I have a function with a Generic
type in the return or in the arguments, void
is a great way to indicate nothing is returned or provided.