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.