# The never Type

> The type of values that should never occur.

Subject: TypeScript | Lesson 3 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/never-type
Previous: [any vs unknown](https://codewithanimation.in/typescript/any-vs-unknown.md) | Next: [The void Type](https://codewithanimation.in/typescript/void-type.md)

## The Impossible Type

`never` is TypeScript's bottom type. It represents a state that is mathematically impossible to reach.

A function that always throws an error or runs in an infinite loop has a return type of `never`. More importantly, `never` is used to represent the remaining possibilities when you have exhausted all other options in a union type.

## Exhaustive Checking

The true power of `never` comes from exhaustive type checking. Imagine you have a discriminated union of shapes (`Circle | Square`). You write a switch statement that handles both.

What happens when a teammate adds `Triangle` to the union six months later, but forgets to update your switch statement? If you assign the unhandled shape in the `default` block to a `never` variable, TypeScript will instantly throw a compiler error because `Triangle` cannot be assigned to `never`.

## Using never to catch bugs

You enforce exhaustiveness by creating a dummy variable typed as `never` in your default case.

```typescript
type Shape = "circle" | "square";
function getArea(shape: Shape) {
  switch (shape) {
    case "circle": return Math.PI;
    case "square": return 4;
    default:
      // If shape is fully handled, 'exhaustiveCheck' compiles.
      // If 'triangle' is added later, TS throws an error here.
      const exhaustiveCheck: never = shape;
      return exhaustiveCheck;
  }
}
```

## Common mistake: The silent runtime crash

Without the `never` assignment, the default block often just throws a generic runtime error: `throw new Error('Unknown shape')`.

While throwing an error is good practice, doing *only* that means you won't discover you missed a case until the code actually runs in production and hits that block. `never` catches it while you are still typing in the editor.

## Tip: A reusable assert helper

Instead of writing the dummy assignment every time, you can create a reusable helper function: `function assertUnreachable(x: never): never { throw new Error('Unreachable: ' + x); }`. Call this in your default cases.

## Try it: Try it

Watch what happens when a new `Triangle` type is added to the system, but the developer forgets to update the switch statement. The `never` check catches it instantly.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/never-type

## Check yourself

### Question 1

When does TypeScript assign a variable the 'never' type implicitly?

- A. When a variable is initialized as null.
- B. When type guards narrow a union type until no possible types remain.
- C. When a function returns undefined.

Correct answer: B

If you have a union 'A | B' and you write an if/else chain that eliminates A and eliminates B, any code running after that elimination will see the variable typed as 'never'.

- Why not A: That results in 'null' or 'any' depending on strictNullChecks.
- Why not C: That function has a return type of 'void' or 'undefined', not 'never'.

## Remember this

- `never` represents values that can never occur, or functions that never return.
- It is the ultimate tool for exhaustive checking in `switch` statements.
- Assigning an unhandled case to `never` ensures the compiler alerts you if you add a new type to a union later.
