Search⌘ K

The Primitive Type never

Discover how the primitive never type in TypeScript represents code paths that never occur, such as exceptions or infinite loops. Learn how it aids exhaustive checks and enforces type safety by preventing unintended returns or unhandled cases in your programs.

We'll cover the following...

The type never means that nothing occurs. It is used when a type guard cannot occur or in a situation where an exception is always thrown. There is a difference between void and never. A function that has the explicit return type of never won’t allow your code to return undefined, which is different from a void function that allows code to return undefined.

TypeScript 3.3.4
function functionThrow(): never {
throw new Error("This function return never");
}

The never type is a subtype for every type. Hence, you can return never (for example, throwing an exception) when a return type is specified to be void or string, but cannot return a string when explicitly marked as never.

TypeScript can benefit from the never type by performing an exhaustive check. An exhaustive ...