Site icon Web Developer Blog

The never Type in TypeScript – Explained with Examples

Let’s break down never in TypeScript in detail with examples, subtype/supertype explanation, and a comparison table with any and unknown.

TypeScript has some special types that play a key role in its type system. One of the most powerful — and sometimes confusing — is never.

At first glance, it looks abstract, but understanding it helps you write safer, more predictable code.


What is never?


never in the Type Hierarchy

let neverValue: never;

// neverValue = "Hello";  // ❌ Error
// neverValue = 123;      // ❌ Error

// But never can be assigned to other types:
let str: string;
let num: number;

function fail(): never {
  throw new Error("This will never return");
}

str = fail(); // ✅ OK
num = fail(); // ✅ OK

Example 1: Functions that Always Throw

A function that throws an error never returns a value → its return type is never.

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

Example 2: Functions with Infinite Loops

A function that never ends also has return type never.

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

Example 3: Exhaustiveness Checking with never

never is very useful in type-safe switch statements.

type Shape = { kind: "circle"; radius: number } 
           | { kind: "square"; size: number };

function getArea(shape: Shape): number {
  switch (shape.kind) {
    case "circle":
      return Math.PI * shape.radius ** 2;
    case "square":
      return shape.size * shape.size;
    default:
      // If we add a new shape and forget to handle it,
      // TypeScript will detect an error here.
      const _exhaustiveCheck: never = shape;
      return _exhaustiveCheck;
  }
}

If you later add { kind: "triangle" } to Shape, TypeScript will force you to handle it in the switch.


Comparison: any vs unknown vs never

Featureanyunknownnever
Meaning“Can be anything”“Can be anything, but must be checked before use”“Can never exist”
Supertype / SubtypeBoth supertype and subtype of all typesSupertype of all types (subtype only of itself and any)Subtype of all types (supertype of nothing)
Assignable to all types✅ Yes❌ No✅ Yes
Accepts all values✅ Yes✅ Yes❌ No
Typical usageQuick prototyping, legacy JSAPI responses, unknown dataImpossible code paths, functions that don’t return
Safety❌ Unsafe✅ Safe✅ Very strict

Practical Use Cases for never

  1. Error-throwing functions function crash(): never { throw new Error("Something went wrong!"); }
  2. Infinite processes (e.g., server loop) function listenForever(): never { while (true) { // keep listening to messages } }
  3. Exhaustive type checking in switch or if-else chains
    Ensures all cases are handled in discriminated unions.
  4. Helper for impossible cases function assertNever(x: never): never { throw new Error("Unexpected object: " + x); }

Conclusion

👉 If any means “everything” and unknown means “something, but check first”, then never means “nothing at all”.


Exit mobile version