runtime.boot

Full Stack Systems
Production Mindset

CodeWithMihir

Engineering thoughtful products from interface to infrastructure.

CodeWithMihir

TypeScript Tutorial

TypeScript never Type Explained with Examples

Learn the TypeScript never type, where it appears, how it works with throwing functions, exhaustive checks, and impossible states.

Welcome back! I am Mihir, and in this lesson we will learn the never type in TypeScript.

never means a value should never exist.

It is used for impossible states, functions that never return, and exhaustive checks.


What is never?

never represents a value that cannot happen.

let value: never;

You cannot assign normal values to it:

value = "hello";
value = 123;
value = true;

All of these are invalid.


Functions That Always Throw

A function that always throws an error never returns normally.

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

Because the function always throws, TypeScript understands the return type is never.


Functions That Never Finish

A function with an infinite loop can also return never.

function runForever(): never {
  while (true) {
    console.log("Running...");
  }
}

This function never reaches the end.


never in Exhaustive Checks

never is very useful with union types.

type Status = "loading" | "success" | "error";

function getMessage(status: Status): string {
  switch (status) {
    case "loading":
      return "Loading...";
    case "success":
      return "Done";
    case "error":
      return "Something went wrong";
    default:
      const impossible: never = status;
      return impossible;
  }
}

The default block should never run because all possible statuses are handled.


Why Exhaustive Checks Help

Imagine you add a new status:

type Status = "loading" | "success" | "error" | "empty";

If you forget to handle "empty" in the switch, this line warns:

const impossible: never = status;

That warning tells you a case is missing.

This is one of the best real-world uses of never.


never vs void

void means a function returns nothing useful.

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

This function finishes normally.

never means a function does not finish normally.

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

So:

  • void returns, but returns no useful value
  • never does not return normally at all

never with Impossible Branches

TypeScript can narrow a value until nothing remains.

function printValue(value: string | number): void {
  if (typeof value === "string") {
    console.log(value.toUpperCase());
  } else if (typeof value === "number") {
    console.log(value.toFixed(2));
  } else {
    const impossible: never = value;
    console.log(impossible);
  }
}

The final else should be impossible.


Common Mistake: Using never Manually Too Often

Most beginners do not need to write never frequently.

You will usually see it in:

  • throwing functions
  • exhaustive switch checks
  • advanced utility types
  • impossible branches

Do not force never into regular variable types.


Quick Reference Summary

ConceptExample
Throwing functionfunction fail(): never
Infinite loopfunction runForever(): never
Exhaustive checkconst x: never = value
Impossible branchafter all union cases are handled
Different from voidvoid returns normally, never does not

Practice

Create an exhaustive check:

type Theme = "light" | "dark";

function getThemeLabel(theme: Theme): string {
  switch (theme) {
    case "light":
      return "Light mode";
    case "dark":
      return "Dark mode";
    default:
      const impossible: never = theme;
      return impossible;
  }
}

Now add "system" to Theme and see TypeScript warn until you handle it.


What You've Learned

You now understand:

  • What never means
  • Why throwing functions can return never
  • How infinite loops can return never
  • How never helps with exhaustive checks
  • The difference between void and never
  • Why never is useful for impossible states

What's Next?

In the next lesson, we will learn the void Type in TypeScript.

We will focus on functions that perform actions without returning useful data.


Need Help?

  • Have questions, confusion, or want to know more? Contact me

never looks strange at first, but it becomes powerful when your app has many possible states.