Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-global-this should allow window.setTimeout() and setInterval() #2482

Open
n0099 opened this issue Oct 19, 2024 · 4 comments
Open

prefer-global-this should allow window.setTimeout() and setInterval() #2482

n0099 opened this issue Oct 19, 2024 · 4 comments

Comments

@n0099
Copy link

n0099 commented Oct 19, 2024

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

n0099 added a commit to n0099/open-tbm that referenced this issue Oct 19, 2024
…tores/routeUpdating.ts`

* allow using `<script lang="tsx">` in rule `vue/block-lang` for `<WidgetSelectForum>` @ eslint.config.js
$ yarn eslint --fix
@ fe
@sindresorhus
Copy link
Owner

I disagree. The correct way to handle it is:

const id: ReturnType<typeof setTimeout> = window.setTimeout(console.log, 100);

@fregante
Copy link
Collaborator

I had this exact issue yesterday 😅

I wish there was a way to instruct the user on how to proceed in cases like this. Should this specific scenario be handled differently? For example:

  • disable auto-fix in TS files
  • extend the message to specify how to specify the type
  • mention it in the rule's readme

I also had issues regarding globalThis types after running this rule. "Nobody knows" how to make globals available as properties on globalThis (the solution seems to be declare var someGlobal: Type, see xojs/eslint-config-xo-typescript#91)

@github-actions github-actions bot changed the title prefer-global-this: should allow window.setTimeout() and setInterval() prefer-global-this should allow window.setTimeout() and setInterval() Oct 20, 2024
@n0099
Copy link
Author

n0099 commented Oct 20, 2024

In my scenario I'm storing the id returned by setTimeout() for latter cancelation via clearTimeout().
If I have to make it compatible with both node and browser enviornment, the initial value of id before its first assign from setTimeout() should be conditionally set to:

let id = import.meta.client ? 0 : import.meta.server ? new NodeJS.Timeout() : ...;

for preventing let TDZ and a violation of eslint rule init-declarations.
Or with the fact that clearTimeout() accepts undefined on both node and browser:

let id: ReturnType<typeof setTimeout> | undefined = undefined;

but this will also violates the eslint rule no-undef-init and unicorn/no-useless-undefined.

@fregante
Copy link
Collaborator

fregante commented Oct 20, 2024

You don't need any of that. Just call setTimeout normally.

As for the let, the lint rule is right. This is correct:

let id: ReturnType<typeof setTimeout> | undefined;

// and then

id = setTimeout()
clearTimeout(id)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants