Typescript Never Type

The Typescript Never type represents the type that never happens or the values that never occur. The following are such places where we use the never type

  1. The return type of function that never returns (infinite loop or always throws an error)
  2. Variables when narrowed by a Type guard that can never be true

Functions that never return

There are a few scenarios where functions might not return. The following examples show two such scenarios. The first example is an infinite Loop and the second example throws an error. In both examples, the function never returns

TypeScript always infers the type as void for function declarations if it cannot determine the return type. You can make the return type as Never as shown below.

Type guard that can never be true

We use Type Guards to narrow down the type of an object within a conditional block. The Typescript infers the type as never if the conditional block is impossible to happen

For Example in the code below the variable padding can be either a string or number. Hence the last else branch will never happen. The TypeScript infers the type of padding variable inside the last else branch as never

Characteristics of Never

You can assign Never to every other type.

But you cannot assign any other type to never (except never itself).

The Typescript infers the return type as never if a function expression or arrow function.

  1. has no return type annotation
  2. has no return statement
  3. Or has a return statement which returns never
  4. does not have an endpoint

(for function declarations void is the default return type)

In the following examples, the TypeScript infers the return type as never.

Not here as we specifically annotate the return type as void

If a function has its return type annotated with never.

  1. All of its return statements (if any) must return never 
  2. The endpoint of the function must not be reachable.

The TypeScript compiler throws error in the following examples as the both the functions are annotated with never type. Because the first example returns a string and the second one has end point.

Void Vs Never

The never type looks very similar to void.

We use void when the function does return but does not return a value. The typescript infers the return value as void.

In the following example, the arrow function does not return anything. But it does finish and return the control to back to the main program. The Typescript infers the return type as void.

The never return type when the function does not return at all. As in the following example, which enters into an infinite loop.

The void type can have undefined or null as a value where as never cannot have any value

References

  1. Type System never
  2. TypeScript Release notes

Read More

  1. Typescript Type System
  2. Type Annotations in TypeScript
  3. Type Inference in typescript

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top