Skip to main content

Void Type

What void means

void is the return type of a function that doesn't return a value — it runs for its side effect and gives nothing back.

function log(msg: string): void {
console.log(msg);
// no return statement
}

void and undefined are not the same

At runtime a void function does return undefined — but the types mean different things:

  • undefined — "returns the value undefined, and I'm being explicit about it."
  • void — "the return value is meaningless; don't use it."

The practical difference shows up in callbacks. A void return type means "I don't care what you return — I'll ignore it." An undefined return type would force the callback to return exactly undefined.

type Cb = () => void;
const cb: Cb = () => 42; // Valid: allowed — the returned 42 is simply ignored

type CbU = () => undefined;
const cbU: CbU = () => 42; // Error — must return undefined

void = "ignore my return." undefined = "my return is literally undefined." That callback flexibility is exactly why Array.forEach takes a void-returning callback.