Avatar ·

TypeScript: how to properly use setTimeout (window vs Node.js)?

Hi everyone! I've just started learning TypeScript and encountered a confusing issue when using `setTimeout`. I'm writing a simple timer for a web application, but TypeScript is complaining about types. Here's my code: ```typescript const timerId = setTimeout(() => { console.log("3 seconds have passed"); }, 3000); clearTimeout(timerId); ``` TypeScript throws an error: "Argument of type 'number' is not assignable to parameter of type 'Timeout'". I'm puzzled. In JavaScript, `setTimeout` always returned a number (numeric timer ID), and I could safely pass it to `clearTimeout`. Why doesn't this work in TypeScript? I googled it and found out that there are two different `setTimeout` functions: one for the browser (window.setTimeout) and one for Node.js (global.setTimeout). They return different types: in the browser it's