void is another type that doesn't exist in JavaScript. It is generally used to represent a non-returning function.
Let's look at an example:
- Let's enter the following function into the TypeScript playground:
function logText(text: string): void {
console.log(text);
}
The function simply logs some text into the console and doesn't return anything. So, we've marked the return type as void.
- If we remove the return type annotation and hover over the function name, logText, we'll see that TypeScript has inferred the type to be void:
This saves us a few keystrokes while writing functions that don't return anything.