never (TypeScript)

`never` is the type of values that never occur. It is the return type of functions that never finish normally — those that always throw or loop forever — and the type of impossible cases.

TypeScript

A function that always throws an error or runs an infinite loop returns `never`, because it never actually returns a value to the caller.

`never` also appears in exhaustiveness checks: in a switch over a union, the 'default' branch can assign the value to a `never` variable, so TypeScript errors if you ever forget to handle a case.

Examples

TypeScript

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

Learn it in a lesson

See also

Back to the Dictionary