Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Primitive and Special Types

The void type in TypeScript

Learn the TypeScript void type: what it means as a return type, how it differs from undefined, and why callbacks typed void may still return values.

void is the return type of a function that does not hand back a useful value. A function such as function log(msg: string): void may finish without a return statement, or with a bare return;. Callers are told not to rely on any result.

void looks like undefined, but it is not the same. undefined is a real type with exactly one value. void says "ignore whatever comes back". That difference is why a callback typed as returning void can still be given a function that returns something.

TypeScript
function greet(name: string): void {
  console.log(`Hello, ${name}`);
}

Try it

TypeScript void return types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Callbacks typed as void may return values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
function name(params): void { /* ... */ }
type Handler = (event: Event) => void;
let v: void = undefined;
Part Description
void A return type meaning "no meaningful result".
Values The only value you can assign to a variable of type void is undefined (and null when strictNullChecks is off).

Edge cases:

  • A function body declared : void cannot return a value; the compiler reports that the type is not assignable to void.
  • A function type () => void accepts functions with any return type. This is called substitutability, and it is what makes arr.forEach(x => set.add(x)) compile even though add returns the set.
  • A function with no return annotation and no return value has the inferred return type void.

Usage notes

  • Use void for return types only. Variables, parameters and properties of type void are almost always a mistake. For a value that may be missing, use undefined.
  • void versus undefined. A function annotated (): undefined historically had to return undefined explicitly (newer versions relax this). A function typed (): void may simply end.
  • Do not test the result. The compiler stops you from using a void value in a condition such as if (log()), because the result is meaningless.
  • Callbacks. When you write your own API that takes a callback whose result you ignore, type it => void. Callers can then pass list.push style functions without wrapping them. If you do need the result, say so with a real return type.
  • Promise<void>. An async function that returns nothing has the type Promise<void>. See Promise<T>.
  • Contrast with never. never means the function does not return at all (it throws or loops forever). void means it returns, but with nothing useful.
  • Not the JavaScript operator. The void operator in JavaScript (void 0) is a separate feature that evaluates an expression and yields undefined.