The Primitive Type never
In this lesson, you will see the type never, which is used to indicate that something must never happen.
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
.
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 ...