Skip to content

[no-misused-promises] async react callback properties when void expected #4619

Closed
@jlowcs

Description

@jlowcs
  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "rules": {
    "no-misused-promises": ["error"]
  }
}
			<button
				type="button"
				onClick={async () => {
					await doSomethingAsync();
				}}
			>
				action
			</button>

or

			<button type="button" onClick={doSomethingAsync}>
				action
			</button>

Expected Result

No linting error.

Actual Result

A linting error is being reported:

src/app/App.tsx
  129:13  error  Promise-returning function provided to attribute where a void return was expected  @typescript-eslint/no-misused-promises

One way to avoid this error would be to wrap the callback's content in an async IIFE:

			<button
				type="button"
				onClick={() => {
					(async () => {
						await doSomethingAsync();
					})();
				}}
			>
				action
			</button>

But that's not really practical when the callback is an existing function (like the 2nd example above).

Additional Info

I guess this new behavior is kind of expected, but it's probably gonna breaks linting on a lot of React projects (including ours, which has now over 100 erros following this update). I'll welcome any suggestion as to how to address it without simply disabling the rule (and risk missing some potential bugs that were detected before).

Versions

package version
@typescript-eslint/eslint-plugin 5.13.0
@typescript-eslint/parser 5.13.0
TypeScript 4.5.5
ESLint 8.10.0
node 16.13.2

Metadata

Metadata

Labels

accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy