eslint-plugin-unicorn: `prefer-global-this` should allow `window.setTimeout()` and `setInterval()`
Description
as it’s a common workaround: 1, 2, for globalThis.setTimeout() and setInterval() returning NodeJS.Timeout in @types/node
Fail
const id: number = window.setTimeout(console.log, 100);
// ^^^^^^
// Prefer `globalThis` over `window`. eslint(unicorn/prefer-global-this)
Pass
const id: number = window.setTimeout(console.log, 100);
const id: NodeJS.Timeout | number = globalThis.setTimeout(console.log, 100);
Additional Info
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 3
- Comments: 17 (1 by maintainers)
Indeed, I ran in to
no-varas well today.😂 Yeah let’s do a separate issue for those things, I’ll just eslint-disable for now.@n0099 Yes I do understand Web API and Node.js API and that’s exactly why I’m here. I still think using
window.setTimeoutis the most clear way, but anyways.Yes it will work if the only usage is for clearInterval. However my function here requires a
Guess I need to change
numbertype.numbertoReturnType<typeof setTimeout>, but that’s just so weird🫠I’ve tried all the different combinations, it will always default to Node.js’s version🫠 eslint-disable for now I guess

It won’t work. It will always default to NodeJS’s version unless you explicitly specify

window.setXxxYou don’t need any of that. Just call setTimeout normally.
As for the let, the lint rule is right. This is correct:
I disagree. The correct way to handle it is: