Since void always returns undefined, and void always evaluates the expression next to it,
you have a very terse way of returning from a function without returning a value,
but still calling a callback for example:

// returning something else than undefined would crash the appfunctionmiddleware(nextCallback){if(conditionApplies()){returnvoidnextCallback();}}

Which brings me to the most important use case of void: It’s a security gate for your app. When
your function is always supposed to return undefined, you can make sure that this is always the case.

button.onclick=()=>voiddoSomething();

void in TypeScript

void in TypeScript is a subtype of undefined. Functions in JavaScript always return something.
Either it’s a value, or undefined:

functioniHaveNoReturnValue(i){console.log(i)}// returns undefined

Since functions without a return value always return undefined, and void always returns undefined
in JavaScript, void in TypeScript is a proper type for telling developers that this function returns
undefined:

declarefunctioniHaveNoReturnValue(i:number):void

void as type can also be used for parameters and all other declarations. The only value that can be
passed is undefined:

So void and undefined are pretty much the same. There’s one little difference though, and
this difference is significant: void as a return type can be substituted with different types, to
allow for advanced callback patterns:

functiondoSomething(callback:()=>void){letc=callback()// at this position, callback always returns undefined//c is also of type undefiend}// this function returns a numberfunctionaNumberCallback():number{return2;}// works 👍 type safety is ensured in doSometingdoSomething(aNumberCallback)

This is desired behaviour and often used in JavaScript applications. Read more on this
pattern called substitutability in my other articles.

If you want to make sure to pass functions who only return undefined (as in “nothing”),
be sure to adapt your callback method signature: