Course contentsShow
← TypeScript
Lesson 946 of 1,79622. The never TypeFree lesson

never vs void Distinction

Why void means 'returns undefined' while never means 'never returns at all' - a critical difference.

never vs void Distinction

You'll learn: Why void means "returns undefined" while never means "never returns at all" — a critical difference that affects how you type functions.

The Core Difference

In TypeScript, both void and never describe functions that don't give you a meaningful value back, but they mean completely different things:

  • void means: "This function runs, completes normally, and returns undefined"
  • never means: "This function never completes — it either throws an error or runs forever"

Think of it this way: void is like a runner who crosses the finish line empty-handed. never is like a runner who never crosses the finish line at all.

Why This Matters

When you declare a function's return type as void, you're saying "this function will execute its code and return control to the caller." The caller can continue executing.

When you declare a function's return type as never, you're saying "if you call this function, execution stops here — either through an exception or an infinite loop." The caller cannot continue normally.

The Mental Model

// void: Completes normally, returns undefined
function logMessage(msg: string): void {
  console.log(msg);
  // Implicitly returns undefined
}

// never: Never completes normally
function throwError(msg: string): never {
  throw new Error(msg);
  // Execution stops here - no return
}

// never: Runs forever
function infiniteLoop(): never {
  while (true) {
    // Never exits
  }
}

After calling a void function, your code continues. After calling a never function, your code cannot continue — TypeScript knows this and uses it for control flow analysis.

Key Takeaway: void functions complete and return undefined; never functions never complete at all — use never for functions that throw errors or loop forever, not functions that simply don't return a value.