Categories
TypeScript Answers

How to get the return type of the setInterval function with TypeScript?

Spread the love

Sometimes, we want to get the return type of the setInterval function with TypeScript.

In this article, we’ll look at how to get the return type of the setInterval function with TypeScript.

How to get the return type of the setInterval function with TypeScript?

To get the return type of the setInterval function with TypeScript, we can use the ReturnType type.

For instance, we write

const t: ReturnType<typeof setInterval> = setInterval(() => {
  //...
});

to set t to the ReturnType<typeof setInterval> type.

typeof setInterval will make the TypeScript compiler automatically detect the type of setInterval.

And using ReturnType on that will make it automatically deduce its return type.

Conclusion

To get the return type of the setInterval function with TypeScript, we can use the ReturnType type.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.