New Primitive Type Never in Typescript

Posted By : Manisha Kirodiwal | 23-Dec-2018

In TypeScript 2.0, a new primitive type was called never introduced. It denotes the value types that never occur. In the following two places we can use Never type:

  • As a return type of functions that never return.
  • Like the type of variables under the type of guards that are never true.

 

These are the exact properties of the never type as described here:

  • Never is a subtype of and acceptable for each type.
  • No type is a subtype or can never be assigned (except never myself).
  • In a function term or arrow function without annotation type return, if the function does not have return statements, or returns only statements of the never-type and if the function endpoint cannot be reached (as determined by control flow analysis), the derived return type of the function is never.
  • In a function with an explicit genotype annotation, all return settings (if any) should never have objects of this type and the function endpoint must not be reached.

 

Following is the example of a function that never returns:

The term body consists of an infinite loop that does not contain any pause or return. There is no way to break out of the loop that the console does not throw. Therefore, it is never derived from the function's return type.

Similarly, the return type of the following function is given as never:

TypeScript never introduces the type because the function does not have either an annotation of the return type or an achievable end point as determined by control flow analysis.

 


Variables with Impossible Types

Another case where the type is never derived is within the type of guards, which is never true. In the following example, we check if value parameters are both a string and a number, which is impossible:

 


The Difference Between never and void

You might ask yourself why TypeScript never needs a type when it already has an invalid type. Although the two may seem the same, they represent two different concepts:

  • A function that does not explicitly return a value implicitly implies that the value is undefined in JavaScript. Although we say that such a function "does not return anything", it returns. Such a function is derived from having an invalid return type in TypeScript.
  • A function that has a return type never returns. It also does not return undefined. The function does not have a normal termination, which means that it throws an error or never runs.

If you are interested in the type of theory, the type is never a bottom type, also known as a zero type or an empty type. It is often called ⊥ and signals that a calculation does not return a result to its caller. The cavity type, on the other hand, is a unit type (a type that allows only one value) without any defined operations.

 

About Author

Author Image
Manisha Kirodiwal

Manisha is a Web Application developer and she is good in working with Angular, TypeScript , JavaScript and Jquery. In free time she loves to dance and cooking.

Request for Proposal

Name is required

Comment is required

Sending message..