BeCuriouzBeCuriouz

Learn something new every day

🔷 TypeScript
🚫

void vs never: Returning Nothing Isn't the Same as Never Returning

💡 void means a function returns but with no useful value; never means it never returns at all.

Not every function hands back a value. In TypeScript, two special return types describe "no value" — but they mean very different things. void means the function finishes normally but returns nothing useful. A logger is the classic case: it does its job and control comes back to you.

ts
function logMessage(msg: string): void {
  console.log(msg);
  // no return, or a bare `return;`
}

never means the function never finishes at all — control never comes back. This happens when a function always throws, or loops forever. There is no return value because execution simply never reaches the end.

ts
function fail(msg: string): never {
  throw new Error(msg); // control never returns
}

function loopForever(): never {
  while (true) { /* ... */ }
}

The key difference: a void function does return to the caller (it just gives back undefined), while a never function does not. That's why never is the bottom type — it's assignable to every other type but nothing (except never) is assignable to it. TypeScript uses this for exhaustiveness checking: in a switch, if you've handled every case, the leftover value narrows to never, and passing it to a never-typed helper proves at compile time you didn't miss one.

🚫 void vs never: Returning Nothing Isn't the Same as Never Returning — BeCuriouz