The Never Type

What

The never type represents values that can never occur. It is used to signal that a piece of code is unreachable or that a variable has been narrowed down to no possible values.

How

A common pattern is to assign a variable to never at the end of a conditional block to ensure every possibility in a union was handled:

function handleStatus(code: 200 | 404) {
  if (code === 200) return "OK";
  if (code === 404) return "Not Found";
  
  // This will error if a new code (e.g. 500) is added to the union
  const exhaustiveCheck: never = code;
  return exhaustiveCheck;
}

Why

never is essential for exhaustiveness checking. If you add a new type to a union, TypeScript will throw a compiler error wherever you’ve used this pattern, reminding you to update your logic to handle the new case.

Summary

Use never to let the TypeScript compiler verify that your logic covers every possible branch of a union.