TypeScript never Type: In-depth Explanation with Examples

Robin
Updated on February 27, 2023

TypeScript is a strictly typed language that's why when we define a variable, we also need to specify the type of value it will store. It has many basic types, such as string, number, and boolean.

However, TypeScript also provides a "never" type, it represents a value that never occurs. This special type has some specific purposes from ensuring type safety to preventing errors in your code.

In this blog post, we'll take an in-depth look at TypeScript "never" type, exploring what it is, how it works, and how you can use it in your own code with some practical examples.

What is never Type in TypeScript

The "never" type in TypeScript is a type that represents a value that never occurs. Essentially, this type can be used if a function will never return anything or if a variable can never be assigned a value.

One common use case for the "never" type is in functions that never return anything. In TypeScript, you can define the return type of this function as "never" to indicate that it will never return anything.

For example, let's say you have a function that is supposed to throw an error if a certain condition is not met.

          function throwError(message: string): never {
    throw new Error(message);
    // This line will never execute
}
        

In this example, the throwError function takes a parameter message as a string and throws an error with that message. As you are throwing an error, this function will not reach its endpoint.

The program will stop executing this function as soon as it throws the error. Because the function never actually completes its execution and returns anything, you can define its return type as "never".

Let's see another example.

          function runForever(): never {
    while (true) {
        console.log('This function will never end')
    }
}
        

In my runForever() function, I am using an infinite while loop. That means when I run this function, it will never end. By defining never as the return type, I am indicating to TypeScript that this function will never actually return a value.

Also Read: Mastering Type Casting in TypeScript: A Complete Guide

Difference Between never and void Type

There is another type called void in TypeScript. You can use it with a function to indicate that this function doesn't return a value. You might think it is similar to the never type but there is a little difference between them.

The main difference between void and never type is that we use void as a return type of a function when we don't explicitly return a value after executing that function completely. On the other hand, we use never when a function never returns a value because it never completes.

          function logMessage(message: string): void {
  console.log(message);
}

const returnValue = logMessage("Hello world!")
console.log(returnValue);
// undefined
        

My logMessage() function has void as its return type because I am not returning anything from this function. But when you execute such a function, it will return undefined by default.

That means this logMessage() function has been executed fully. You will use never type for a function that never gets the chance of returning something because it won't reach the endpoint.

Conclusion

The "never" type is a relatively advanced feature of TypeScript, and you might not use it in everyday programming. But it can be useful in some unique situations. That's why I showed you some examples so that you understand where and how to apply this type.

When you know the usage of different types in TypeScript and understand the differences among them, you will feel comfortable while writing TypeScript code in your project.

If you use proper types in your code, it will increase the readability and maintainability. Because you will easily understand what a piece of code does by seeing the types. That's why in this post, we've explored the concept of the "never" type in TypeScript and its various use cases.

Related Posts