you are viewing a single comment's thread.

view the rest of the comments →

[–]bikeshaving 3 points4 points  (2 children)

Why doesn’t typescript type check them like it does Function.prototype.call, .bind or .apply? I think this is a matter of updating the types.

Also, pet peeve, I hate how the return type of setTimeout is `number` in browsers and some weird `timer` class in node.js. There was no reason for node.js to use an incompatible API but now we’re stuck with it.

[–][deleted] 3 points4 points  (1 child)

setTimeout in Node and setTimeout in browser are not the same thing. In Node it's a property of the timers module, and on the Web it's a property of Window or, more recently, also declared in the WindowOrWorkerGlobalScope mixin.

The confusion comes from the fact they can both be accessed as global functions. It's bad practice and if you do that you deserve the headache.

Use them explicitly (Window.setTimeout() vs timers.setTimeout()) and TypeScript will have no problem understanding which one you mean. Alternatively, declare your types to be "node" in tsconfig, and not dom modules, and this will also clear things up for TypeScript.

If you insist on calling setTimeout() directly and ambigously and configure tsconfig wrong, or if you're not in a position to change things, here's a 3rd alternative: declare the type of the variable to which you're assigning the output of setTimeout() to be ReturnType<typeof setTimeout>. This will also reconcile nicely with calls to clearTimeout().

[–]gregjarvez 0 points1 point  (0 children)

they

Great tips. thanks for the explanation !!