Explain the purpose of never type in TypeScript


TypeScript is a type-strict language in which we need to define the type for every variable. Also, we need to define the return type of the function and the types of parameters of the function.

The never is also one of type in TypeScript like other data types such as string, number, boolean, symbol, etc. We can use the ‘never’ keyword to make a variable of never type.

Users can use the never type when they are sure about any situation that will never occur. For example, we can use the never as a return type when we are sure that the function will never return any value.

Syntax

Users can follow the syntax below to use the never type as literal for variables.

// never type as a variable literal
let variable: never;

// never type as a function return type
function func(): never {

   // write any condition such that the function shouldn't return any value
}

In the above syntax, the variable type is never, which means we can never store any value to the variable. Also, the function's return type is never, which means we can never return value from the function.

Example 1: Useing the never keyword as a literal

In the example below, we have defined the variable and used the never keyword as a literal. After that, users can see that we can’t assign value to the variable of a type never. If we try to assign value to the variable of type never, the TypeScript compiler generates an error.

// never type as a variable literal
let variable: never;

// The below code generates the error message like Type 'number' is not assignable to type 'never'. 
// variable = 10;

// we can't print the variable before initializing it.
// console.log(variable);

// This will not work as we can't assign the value to the variable of never type
// let variable2: never = 10;

console.log("Working the variable of type never")

On compiling, it will generate the following JavaScript code −

// never type as a variable literal
var variable;

// The below code generates the error message like Type 'number' is not assignable to type 'never'. 

// variable = 10;

// we can't print the variable before initializing it.

// console.log(variable);

// This will not work as we can't assign the value to the variable of never type

// let variable2: never = 10;
console.log("Working the variable of type never");

The above code will produce the following output −

Output

Working the variable of type never

Example 2: using the "never" as a return type of the function

In the example below, we have created the sample and test function. The sample function runs the while loop for infinite times, and it will never stop. The test function also runs the for loop for infinite times, so it will never return value, and we can use the never as a return type of the function.

Also, users can see that sample() function will never be called by TypeScript, as the execution of the test() function will never stop.

// defining the sample function, which never returns a value and never stops execution
function sample(param1: string): never {
   while (true) {
      console.log("The value of the param1 is " + param1);
   }
}

// test function, which will return for loop for infinite time
function test(param2: number): never {
   for (let i = 0; ; ) {
      console.log("The value of param2 is " + param2);
   }
}

// calling the test function
test(20);

// calling the sample function

// sample function never invoked, as the test function will never stop
sample("Hello");

On compiling, it will generate the following JavaScript code −

// defining the sample function, which never returns a value and never stops execution
function sample(param1) {
    while (true) {
      console.log("The value of the param1 is " + param1);
   }
}

// test function, which will return for loop for infinite time
function test(param2) {
   for (var i = 0;;) {
      console.log("The value of param2 is " + param2);
   }
}

// calling the test function
test(20);

// calling the sample function

// sample function never invoked, as the test function will never stop
sample("Hello");

Example 3

In this example, we have created the error function, which always throws an error. So, it will never return any value from the function. After that, we invoked the error() function from the sample() function.

In the output, users can observe that the below code shows an error.

//  function that always throws an error
function error(): never {
   throw new Error("Errors in the return type!");
}

// calling the error() function from the sample() function
function sample() {
   return error();
}

sample();

On compiling, it will generate the following JavaScript code −

//  function that always throws an error
function error() {
   throw new Error("Errors in the return type!");
}

// calling the error() function from the sample() function
function sample() {
   return error();
}
sample();

The above code will produce the following output −

Output

Error: Errors in the return type!

Use never type as a union or intersection with other data-types

We can use the never type as a literal with other data types such as number, string, or boolean. If we take the union of never type and number type, the variable becomes number type.

If we take the intersection of the never type and number type, the never type always overrides, and the variable becomes the never type.

Syntax

Users can follow the syntax below to use the never type as a union and intersection of another data type.

let var1: never | number;
let var2: never & number;

In the above syntax, var1 is either of never or number type, and var2 is a type of never and number, which means the never type always overrides the number type.

Example 4

In the example below, users can see that we can assign the number value to the var1 as the number type overrides the never type. Also, we can’t assign value to the var2 as the never type overrides the number data type.

let var1: never | number;
let var2: never & number;

// we can assign value to the var1 as it is a type of number
var1 = 30;

//  we can't assign value to the var2 as it is a type of never
// var2 = 30;

console.log("The value of var1 is " + var1);

On compiling, it will generate the following JavaScript code −

var var1;
var var2;
// we can assign value to the var1 as it is a type of number
var1 = 30;

//  we can't assign value to the var2 as it is a type of never

// var2 = 30;
console.log("The value of var1 is " + var1);

The above code will produce the following output −

The value of var1 is 30

Users learned about never type in TypeScript in this tutorial. We have seen the different use cases of never type via different examples. When we use the never as a return type of function, the function should contain any condition which never allows the function to return a value.

Updated on: 17-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements