Open In App

TypeScript never

Last Updated : 18 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn about never in Typescript. In TypeScript, the Never type is a function that never returns a value but throws an exception or error and terminates the program. It is often used to indicate that a function will not return a value or that a variable will never have a value. Here’s a brief overview of never:

Syntax

function throwError(message: string): never {
    throw new Error(message);
}
function infiniteLoop(): never {
    while (true) {
        // Code that never exits
    }
}

Parameters

  • message: string: In the first example, the throwError function takes a message parameter of type string. It throws an error with the provided message and returns never because the function does not complete normally; it throws an exception.
  • No parameters: In the second example, the infiniteLoop function does not have any parameters. It contains an infinite loop and also returns never because it never exits and continues running indefinitely.

The never type is often used in scenarios where TypeScript can infer that a function or variable will never produce a valid value or reach the end of its execution. It’s particularly useful for catching programming errors and indicating that certain code paths are unreachable.

Example 1: This example displays the throwCustomError function that when called gives an Error and does not return any output and the program ends.

Javascript




function throwCustomError(message: string): never {
    throw new Error(message);
}
 
function processResult(result: string | null): string {
    if (result === null) {
 
        // This function throws an error,
        // so the next line is unreachable.
        throwCustomError("Result is null.");
    }
    return result;
}
 
const result = processResult("Hello GeeksforGeeks!");
console.log(result);


Output:

Hello GeeksforGeeks!

Example 2: In this example, the throwError function is defined to throw a custom error with a given message. It has a return type of never because it doesn’t return normally; it throws an exception. The divide function takes two parameters, a and b, and performs division. However, it checks if b is zero and, if so, calls throwError with the message “Division by zero.”.After calling the divide with valid arguments, it computes the result of the division and logs it to the console.

Javascript




function throwError(message: string): never {
    throw new Error(message);
}
 
function divide(a: number, b: number): number {
    if (b === 0) {
        throwError("Division by zero.");
         
        // This function throws an error,
        // so the next line is unreachable.
    }
    return a / b;
}
 
const result = divide(10, 2);
console.log(result);


Explanation: In this case, the division is valid (10 divided by 2 is 5), so the code executes successfully, and the result is logged to the console. However, if you were to call divide(10, 0), it would result in an error being thrown with the message “Division by zero,” and the subsequent code would be unreachable.

Output:

5 

Reference: https://www.typescriptlang.org/docs/handbook/basic-types.html#never



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads