DEV Community

Lautaro Suarez
Lautaro Suarez

Posted on

TypeScript: "type predicates"

What are type predicates?

This question came up while I was going through the TypeScript documentation. Initially, I found it difficult to understand their purpose and when to use them. However, after conducting thorough research and studying practical examples, I finally grasped their significance and the situations in which they are beneficial. With that in mind, I will now make an effort to explain type predicates in a way that is easier to comprehend.

Type Predicates functions

Type predicate functions, also known as type guards, are functions that return a boolean value. They play a crucial role in refining the types within a function, allowing us to have a clear understanding of what we can expect from it. By utilizing type predicate functions, we can effectively narrow down the type of a variable or parameter, enabling more precise type inference and ensuring safer and more reliable code execution.

function isString(x: unknown): x is string {
  return typeof x === 'string';
}
Enter fullscreen mode Exit fullscreen mode

This concept is quite straightforward, and that's precisely why it is so valuable. We have a function called isString that accepts arguments of any type. Inside this function, we perform a type check using the typeof operator to determine if the argument is of type string.

But why do we use the x is string syntax? It serves two purposes. Firstly, it provides context and clarity to the code, clearly indicating that the function is expected to return a boolean value indicating whether the argument is a string or not. Secondly, it follows clean code practices by employing a well-defined naming convention that enhances code readability and maintainability.

Cons and pros

Let's start with the cons:

  • A bad type on the function can result in a non ending debugging error that can take hours even days to understand where the error was.
  • It adds more code to every function.

Example of a really common mistake or error:

function isString(x: unknown): x is number {
  return typeof x === 'string';
}
Enter fullscreen mode Exit fullscreen mode
function isString(x: unknown): x is string {
  return typeof x === 'number';
}
Enter fullscreen mode Exit fullscreen mode

In both cases, there seems to be an error. In the first example, we incorrectly state that we expect x to be a number in order for the function to return true. However, this contradicts the actual code, where we are checking if x is a string.

It's important to ensure consistency between the expected type mentioned in the comment and the actual type check performed within the function. To rectify this, we should update the comment or the type check to accurately reflect the intended behaviour, ensuring that x is checked for being a string. This alignment between the comment and code promotes clarity and helps other developers understand the function's purpose more accurately.

Conclusion

I highly recommend using type predicates when adhering to good coding practices to enhance the readability of your code for other developers. However, it is essential to exercise caution and avoid overusing type predicates, as excessive reliance on them can introduce errors and make it challenging to identify their root causes. Striking the right balance and using type predicates judiciously will contribute to code clarity without compromising code maintainability and debugging efficiency.

Cheers, Lautaro

Top comments (0)