Skip to content

Bug: [prefer-optional-chain] should report case that can be converted to optional function call ?.() #11270

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

Open
4 tasks done
kirkwaiblinger opened this issue Jun 4, 2025 · 1 comment · May be fixed by #11272
Open
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@kirkwaiblinger
Copy link
Member

kirkwaiblinger commented Jun 4, 2025

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.6.3&fileType=.tsx&code=CYUwxgNghgTiAEYD2A7AzgF3gMyUgXPAN4BQ88AtiBgBZLCECuKo2AliiMPAD7wAU-AJTwAvAD54ANyRtgQkgF8SJXEgB0VWvXgAyXTjybqdYMIDcQA&eslintrc=N4AhCgwIgJwVwDYFMDOUBcJiTCKABAFwE8AHVAYxgEtTCBaVBagO0IHpSYkAzJGegHs61QSwCGCehQAW41hjwB3cTBZQcAX3CagA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDAruuGAL4g9A&tokens=false

Repro Code

declare const foo: {
  method: undefined | (() => void)
}

foo.method && foo.method();

ESLint Config

module.exports = {  
  "rules": {
    "@typescript-eslint/prefer-optional-chain": "warn"
  }
}

tsconfig

{
  "compilerOptions": {
    "strict": true
  }
}

Expected Result

I expect that the rule should report and suggest foo.method?.().

Actual Result

No report

Additional Info

This is specifically implied in the docs examples with the line foo && foo.a && foo.a.b && foo.a.b.method && foo.a.b.method();

Relates to #9605

@kirkwaiblinger kirkwaiblinger 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 Jun 4, 2025
@kirkwaiblinger
Copy link
Member Author

Oh, this appears to be a case specifically around the return type being void...

(playground)

declare const foo: {
  returnsVoid: undefined | (() => void)
  returnsString: undefined | (() => string)
}

// does not report
foo.returnsVoid && foo.returnsVoid();
// reports
foo.returnsString && foo.returnsString();

At least in cases where the return value is not used, I'd think we should report these kinds of expressions. This is a prime use case for ?.(), since patterns like context.optionalCallback?.(); arise naturally.

@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants