DEV Community

Mohammad Jamal Dashtaki
Mohammad Jamal Dashtaki

Posted on

Unknown, any and never types in Typescript

Intro

TypeScript is an open-source programming language that adds optional static typing to JavaScript. It is widely used for developing large-scale applications and has several types that help developers to write better code. In this article, we will discuss the differences between the unknown, any, and never types in TypeScript.

Difference

Let's take a look at a simple code example to understand the difference between unknown and any types:

function getValue(): unknown {
  return "Hello, world!";
}

const value: any = getValue();

console.log(value.toUpperCase()); // No error at compile-time, but can throw runtime error
Enter fullscreen mode Exit fullscreen mode

In this example, the getValue function returns a value of type unknown, which means that the type of the value is not yet known during the execution of the program. When we assign the value to a variable value of type any, we tell TypeScript to allow any type of value to be assigned to that variable, including the value returned by getValue.

Now, when we call the toUpperCase method on the value variable, TypeScript does not throw an error at compile-time, but it can still throw a runtime error if the value is not a string. This is because the any type does not provide any type safety, and the developer needs to ensure that the value is of the correct type before using it.

On the other hand, if we change the type of the value variable to unknown, TypeScript will require us to perform a type check before using the value, like this:

function getValue(): unknown {
  return "Hello, world!";
}

const value: unknown = getValue();

if (typeof value === "string") {
  console.log(value.toUpperCase()); // No error
}
Enter fullscreen mode Exit fullscreen mode

In this example, we perform a type check to ensure that the value variable is a string before calling the toUpperCase method. This provides better type safety and prevents runtime errors.

The never type is different from unknown and any types, as it represents values that will never occur. Here's an example:

function throwError(): never {
  throw new Error("Something went wrong!");
}

const result: never = throwError(); // No error
Enter fullscreen mode Exit fullscreen mode

In this example, the throwError function always throws an error, so it will never return a value. We can use the never type as the return type of the function to communicate this to the compiler. This helps in improving the overall code quality and maintainability, as it clearly communicates the intent of the function.

Usecase:

The unknown type in TypeScript should be used when the type of a value is not known at the time of declaration. It is particularly useful when working with dynamic data, such as data from API responses, as it provides better type safety and prevents runtime errors.

The any type should only be used as a last resort, when it is not possible to determine the type of a value. It should be avoided as much as possible, as it can lead to runtime errors and make the codebase difficult to maintain.

The never type should be used as a return type for functions that will never return or throw an error. This helps in making the code more readable and maintainable, as it clearly communicates the intent of the function.

Conclusion:

In conclusion, TypeScript has several types that help developers to write better code. The unknown type provides better type safety and is useful when working with dynamic data. The any type should be avoided as much as possible, as it can lead to runtime errors and make the codebase difficult

Top comments (0)