Codectionary / Developer documentation / TypeScript

never versus void Return Types

void describes a function whose return value is not used, while never describes a function that does not complete normally.

What it is and why to use it

void describes a function whose return value is not used, while never describes a function that does not complete normally. A never-returning function can throw or run forever. Distinguishing them helps the compiler understand that execution cannot continue past a failing branch.

Common mistakes and limitations

void does not mean that a callback can never return a value: a value-returning function can often be assigned to a void-returning callback type because its result is ignored. never is not an ordinary empty value and should not be invented with an assertion.

Run this example

Use TypeScript 5.8 or later with strict checking and an ES2022 target. Save each complete example independently as example.ts, compile it, then run the generated JavaScript with a compatible Node.js installation. Compiler-only options below affect checking; they do not add runtime validation.

Terminal — compile and run

npx tsc example.ts --strict --target ES2022 --module commonjs
node example.js

Syntax

function fail(message: string): never; function log(): void;

Examples

never versus void Return Types

Standalone TypeScript 5.8+ example; expected output is explained below.

function fail(message: string): never { throw new Error(message); }
function report(message: string): void { console.log(message); }
function requireName(name: string | undefined): string {
  if (name === undefined) fail("Name required");
  return name;
}
report(requireName("John"));
  1. fail always throws, so it never supplies a normal result to its caller.
  2. After the undefined branch, TypeScript knows name must be a string.
  3. report writes the name and its caller ignores any return value. A throwing helper can make required-value checks easier to follow.
  4. Expected output: John

Best practices

  • void does not mean that a callback can never return a value: a value-returning function can often be assigned to a void-returning callback type because its result is ignored. never is not an ordinary empty value and should not be invented with an assertion.
  • Keep external-input validation separate from compile-time type descriptions.

Runtime & versions

Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

TypeScript Handbook

Specifications & further reading