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

Bug: [no-invalid-void-type] false positive on this: X | void annotation #10984

Open
4 tasks done
jedwards1211 opened this issue Mar 21, 2025 · 2 comments
Open
4 tasks done
Labels
awaiting response Issues waiting for a reply from the OP or another party bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@jedwards1211
Copy link

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.7.2&fileType=.tsx&code=KYDwDg9gTgLgBDAnmYcAKUIFsCWBnYAdRxgAsAlYPCAGwDdgo8AeAFQD44BeOAbwCg4cAPTC4AOkn8Avv37CAVAsEK4AVQJQaAQwB2AEzg4sYGsCzBdMbTBwRdcCADM4AbQzZ8wcQHcSFKloGJgBdXgABGhxdAGs4UhgYMDwALlF9YAYaCBQocSwIAC8cGh1xaABzYUsAWjUAZWF9CABjPGFCYAAjYQApbTptepaoHDAYYUonRksW4GEAcWyu7RoAfQB5LoArYBaYdo9cAmE-MkpqekY8aXEVOA3dObgfVABXMAqobQyECDgAHIQX4AJhBABoXqgWno4Hgzi1SH8EKRUF03iVbLo7nAFMJ%2BKBILA4E43k9bPYXv4LkFrmx2AAKQQo-ApdCYY7AADC9jwMCgb320DgAB84HQIDh9PwAJRso5eYjnQJXJj0vjMqDAGBvKAOARCISiCRSISyWRAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1uYDcBDRgBNaPDpWFFS6KImjQO0SODABfECqA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

export type PromiseWithResolvers<T> = {
  // ...
}

/**
 * Userland implementation of [Promise.withResolvers]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers}.
 * Once we upgrade to Node 22, we can switch to the builtin.
 */
export function withResolvers<T>(
  this: PromiseConstructor | void
): PromiseWithResolvers<T> {
  return {
    // ...
  }
}

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/no-invalid-void-type": "error",
  },
};

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

No error

Actual Result

void is not valid as a constituent in a union type 10:30 - 10:34

Additional Info

This is the only possible way to type an optional this binding other than function overloads, but function overloads trigger @typescript-eslint/unified-signatures.

@jedwards1211 jedwards1211 added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Mar 21, 2025
@JoshuaKGoldberg
Copy link
Member

🤔 Interesting, I've seen ponyfills and wrappers for withResolvers before but never with a union type for this. Could you please explain why you went with this type? Also, why not PromiseConstructor | undefined?

@JoshuaKGoldberg JoshuaKGoldberg added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for team members to take a look labels Mar 24, 2025
@jedwards1211
Copy link
Author

I don't remember where I got it from, but probably the idea was you could call withResolvers() (TS only accepts this: void in that case, not this: undefined) or withResolvers.call(thirdPartyPromiseConstructor). It may be unusual but it's never fun when it's impossible to satisfy both TS and typescript-eslint, I hope it's always your goal to avoid that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response Issues waiting for a reply from the OP or another party bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

2 participants