Understanding the Difference Between `void` and `never` in TypeScript

  • |
  • 15 June 2025

Understanding the Difference Between void and never in TypeScript

When working with TypeScript, we may encounter various utility types that help define the intent and behavior of values in our code. Two such types that often cause confusion are void and never. While they may seem similar at first glance — both representing the absence of a value — they serve very different purposes in practice. Understanding the distinction between them is essential for writing clearer, safer TypeScript code.

The void Type

The void type represents the absence of a return value from a function. In plain JavaScript, functions that do not explicitly return anything return undefined by default. TypeScript captures this idea with the void type.

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

In the example above, logMessage performs a side effect (printing to the console) but doesn’t return any value. Declaring its return type as void clearly communicates this intent.

Note that a function returning void might still technically return undefined or even null, but TypeScript ensures the value is not expected or used. Attempting to use the return value from a void function typically leads to a type warning.

The never Type

In contrast, never represents values that never occur. A function with a return type of never is one that either throws an error or enters an infinite loop — meaning it never successfully completes or returns a value.

function throwError(message: string): never {
  throw new Error(message);
}

Another example:

function infiniteLoop(): never {
  while (true) {}
}

In both cases, the function will never reach a point where it could return a value. The never type is TypeScript’s way of expressing “this code path does not produce a value at all.”

Key Differences

Aspect void never
Meaning Function returns no value Function never returns
Runtime behavior May return undefined Never completes normally
Use case Functions with no return value Functions that throw or loop forever
Assignability Not assignable to never Assignable to no other type

One practical use of never is in exhaustive type checking with union types:

type Shape = 'circle' | 'square';

function handleShape(shape: Shape) {
  switch (shape) {
    case 'circle':
      // handle circle
      break;
    case 'square':
      // handle square
      break;
    default:
      const _exhaustiveCheck: never = shape;
      // If a new shape is added to Shape type, TypeScript will error here
  }
}

In this example, never helps ensure all cases are covered, providing a safeguard against unhandled conditions in union types.

Conclusion

While both void and never are used to describe the absence of a return value, their meanings and applications are very different. Use void when a function doesn’t return anything useful, and use never to indicate a function that cannot return at all. Mastering these distinctions will improve your ability to write robust and type-safe TypeScript code.

You May Also Like