Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

The void operator and the void type

Reference for void in TypeScript: the return type of functions that return nothing, the callback rule, void versus undefined, and the void operator.

void appears in two roles. As a type, it marks a function that does not produce a useful result. As a JavaScript operator, void expression evaluates the expression and always returns undefined.

The type is the common one: function log(m: string): void says callers should not use the return value. It is close to, but not the same as, undefined: a function whose type returns void may return anything when it is used as a callback, while a function declared to return undefined must return it.

TypeScript
function log(message: string): void {
  console.log(message);
}

Try it

TypeScript void return types and callbacks

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 The void operator

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 f(): void {}
type Callback = () => void;
let v: void = undefined;
void expression
Form Description
: void on a function The function returns no useful value. With an explicit annotation, a return with a value is an error.
() => void as a type Any function can be assigned, whatever it returns. The result must not be used.
void expr Evaluates expr, discards it, gives undefined.

Edge cases and errors:

  • Only undefined (and any, never) is assignable to a variable of type void; with strictNullChecks, null is not.
  • A function expression with a void return type annotation cannot return a value; an arrow function assigned to a void-returning function type can.
  • A void result cannot be tested for truth meaningfully: if (f()) where f returns void is error TS1345.
  • Promise<void> is a promise that resolves with no value; await gives undefined.
  • void and undefined differ in generics: a parameter of type void can be omitted, so resolve() works for Promise<void>.

Usage notes

  • Prefer void as a return type when the function is used for side effects. Use undefined when callers need the value undefined explicitly.
  • Do not use void as a parameter or property type except for generics, where it means "nothing to pass".
  • The operator form is mostly used as void promise to mark an intentional fire-and-forget call (linters flag unhandled promises), and as void 0 in older code for a safe undefined.
  • void is unrelated to never: never means the function does not return at all (it throws or loops).
  • A method declared (): void in an interface can be implemented by a method that returns something, which is why the callback rule exists.