Categories
TypeScript

What is never in TypeScript?

TypeScript is a powerful typed superset of JavaScript that adds a number of features to the language, including the ability to define types for variables, function arguments, and return values. One of the lesser-known types in TypeScript is “never”. In this article, we’ll explore what “never” is and how it can be used in your code.

Understanding the “never” type

The “never” type is used to indicate that a certain block of code or a certain value will never be reached or returned. This is useful in situations where you know that a certain piece of code will throw an error or where a function will always return early.

For example, consider the following function:

function throwError(): never {
    throw new Error("This function always throws an error.");
}Code language: JavaScript (javascript)

In this function, we know that it will always throw an error, so we can annotate the return type as “never”. This tells TypeScript that the function will never return a value and that any code that calls this function should not expect a return value.

Another example of using “never” is in the following function :

function checkAge(age: number): string {
    if (age < 18) {
        return "Too young";
    } else if (age >= 18) {
        return "Old enough";
    }
    else {
        // This code will never be reached
        return "Invalid age"; // TypeScript will know this is impossible
    }
}Code language: JavaScript (javascript)

In this function, we know that the else block will never be reached and thus the return type is “never”.

Differences between “never” and other types

It’s important to note the difference between “never” and other types such as “void” and “any”.

  • “void” is used to indicate that a function does not return a value. In contrast, “never” is used to indicate that a function will never return a value.
  • “any” is a type that can be assigned any value. “never” is the opposite, it can never be assigned any value.

It’s also worth noting that “never” can be used in conjunction with other types. For example, a function that returns “never” may also have a return type of “never | Error”. This tells TypeScript that the function may return an Error object in addition to never returning.

How to use “never” in your code

Using “never” in your code is relatively simple. When you have a block of code or a function that you know will never return a value, simply annotate the return type as “never”.

Here’s an example of a function that throws an error and returns “never”:

function throwError(): never {
    throw new Error("This function always throws an error.");
}Code language: JavaScript (javascript)

And here’s an example of a function that returns “never” if a certain condition is met:

function checkAge(age: number): string | never {
    if (age < 18) {
        return "Too young";
    } else if (age >= 18) {
        return "Old enough";
    } else {
        throw new Error("Invalid age");
    }
}Code language: JavaScript (javascript)

Conclusion

In conclusion, “never” is a powerful type in TypeScript that can be used to indicate that a certain block of code or a certain value will never be reached or returned. It can be used in situations where you know that a certain piece of code will throw an error or where a function will always return early. It’s important to note the difference between “never” and other types such as “void” and “any”, and how “never” can be used in conjunction with other types. Using “never” in your code is simple, just annotate the return type as “never” when appropriate. By using “never” in your code, you can make your code more expressive and easier to understand for other developers.

Leave a Reply

Your email address will not be published. Required fields are marked *