TypeScript

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 represents the type of values that never occur.
  • It is the opposite of any:
    • any means “it could be anything”.
    • never means “it could be nothing”.
  • It is used in functions that cannot produce a value because:
    1. They throw an error.
    2. They run forever (infinite loop).
    3. A variable cannot logically exist in a certain branch.

never in the Type Hierarchy

  • never is a subtype of all types.
  • But it is not a supertype of anything.
  • That means you can assign never to any type, but nothing can be assigned to never (except never itself).
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

  • never is the type of nothing — values that can never exist.
  • It is a subtype of all types but not a supertype.
  • Functions that never return (error, infinite loop) have return type never.
  • It shines in exhaustive checks to ensure type safety in union types.
  • Compared to any and unknown, it provides the strictest safety guarantees.

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


Related posts
TypeScript

Literals in TypeScript: String, Number, Boolean, Template, and Compound Literals

Literals are one of the fundamental building blocks of TypeScript programming. They represent fixed…
Read more
TypeScript

Utility Types in TypeScript: Pick, Omit, Partial, Required, Readonly, Record...

✅ — Utility Types are one of the most powerful features of TypeScript, but also one of the most…
Read more
TypeScript

Complete TypeScript Tutorial Online: Master TypeScript in 2025

TypeScript has revolutionized modern web development by bringing static typing to JavaScript, making…
Read more