The Strange Behavior of the void Type in TypeScript
Posted by stackoverflooooooow@reddit | programming | View on Reddit | 4 comments
Posted by stackoverflooooooow@reddit | programming | View on Reddit | 4 comments
Awesan@reddit
The
void
type signals intent. In the first example where you define a function:You are saying "this function will not return anything". However in the second example:
The "callSay" function is actually saying "I promise not to use the return value of the func". So that's why it's still OK for functions declared like this to return something:
Whoever receives this
sayHello
function has already promised not to use the return type, so it does not matter if you do return something.No-Quail5810@reddit
It's like that because ignoring a return value and having the return type be `void` are essentially the same thing. Besides, technically all functions technically return a value, even if it's just `undefined`
Awesan@reddit
Yes exactly, hence "void" really means "don't care".
andarmanik@reddit
Nicely written