The void Type in Functions
How void differs from undefined in function return positions.
The void Return Type
void describes a function that returns no useful value. The function still completes; it just doesn't produce something you should use. It's the type of functions called for their side effects.
function log(message: string): void {
console.log(message);
}
log('Hello void');Inferred void
If a function has no return statement (or only bare return;), TypeScript infers void automatically. You rarely need to write it explicitly, though it documents intent.
function greet(name: string) {
console.log('Hi ' + name);
// no return -> inferred void
}
greet('Ada');All lessons in this course
- Understanding unknown vs any
- The never Type and Impossible States
- The void Type in Functions
- Type-Safe Handling of unknown