Skip to content

Bug: [@typescript-eslint/require-await] False positive when implementing async interface with sync function #7450

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

Closed
4 tasks done
panstromek opened this issue Aug 10, 2023 · 1 comment
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended

Comments

@panstromek
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.1.6&fileType=.ts&code=LAKAlgdgLgpgTgMwIYGMYAICCBnAnhFAZQAsB7Ad3QG9R071szyAudACgEp0BeAPnQAKcUgFsw2GAB4AbqTAATXqAC%2BoUAgCuBKGFIR0I0igDWOfCgBiETqzMESFarXpwYUDXH00Q9X%2BiR4BAxMnE4%2BfhEoetikADYwAHSxpADmbADkEKQADukczhG%2Bru6eBX6q4XQVFUA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6aRAR1ko9oEMA7v0r50URNGgB7aJHBgAviEVA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

interface AsyncShow {
    show: () => Promise<void>
}

function mockAsyncFn(): AsyncShow {
    return {
        async show() {
            console.log('nop')
            return
        }
    }
}

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/require-await": "error"
  },
};

tsconfig

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

Expected Result

No error.

Actual Result

Error on show implementation: Async method 'show' has no 'await' expression.

Additional Info

Since the method just implement an interface (which might be out of scope), it shouldn't require await keyword if it is not necessary for this specific implementation.

The way to fix this is to use Promise.resolve() inside non-async function, but that's unnecessarily verbose.

@panstromek panstromek 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 Aug 10, 2023
@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 Aug 10, 2023
@bradzacher
Copy link
Member

See discussion on #7536
After consideration - this is working as expected.

Relying on async to lazily satisfy a promise-based contract is generally considered a code smell and is one of the reasons this rule exists.
Ignoring this case would leave the code ambiguous - "did the author forget an await? Did they msirwite the code? Did they intentionally use async to wrap the return in a promise?"

Instead the rule is encouraging you to be explicit and clearly mark your return with a promise (eg Promise.resolve()) which unambiguously signals that the imementatikn intentionally does not contain asynchronous code.

@bradzacher bradzacher closed this as not planned Won't fix, can't repro, duplicate, stale Aug 26, 2023
@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended and removed accepting prs Go ahead, send a pull request that resolves this issue labels Aug 26, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 2, 2023
@bradzacher bradzacher marked this as a duplicate of #11085 Apr 17, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended
Projects
None yet
Development

No branches or pull requests

2 participants