typescript-eslint: [no-misused-promises] non-thenable thenable union react component attribute does not accept thenable function

  • 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": {
    "@typescript-eslint/no-misused-promises": ["error"],
  }
}
interface Props {
  onEvent: (() => void) | (() => Promise<void>);
}

const Component: React.FC<Props> = () => null;

const App: React.FC = () => {
  const handleEvent = async () => {};
  return <Component onEvent={handleEvent} />;
};

export default App;
{
  "compilerOptions": {
    "target": "ESNext",
    "useDefineForClassFields": true,
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "allowJs": false,
    "skipLibCheck": false,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": ["src"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

Expected Result

No linting error since onEvent accepts both a thenable and a non-thenable function.

Actual Result

9:29  error  Promise-returning function provided to attribute where a void return was expected  @typescript-eslint/no-misused-promises`

Additional Information

This bug was introduced in 5.14.0. 5.13.0 ist working fine.

Note that you can circumvent the bug by using

interface Props {
  onEvent: () => void | Promise<void>;
}

instead.

Versions

package version
@typescript-eslint/eslint-plugin 5.14.0
@typescript-eslint/parser 5.14.0
TypeScript 4.6.2
ESLint 8.10.0
node 14.18.3

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 15 (11 by maintainers)

Most upvoted comments

Indeed - this is another edge case I hadn’t thought of. 😞

Fix in review here: #4841

Since it’s a bit buried in the thread, bumping that you can disable the new check added in v4.6 with the settings described at https://typescript-eslint.io/rules/no-misused-promises/#checksvoidreturn:

"rules": {
  "@typescript-eslint/no-misused-promises": ["error", {
    "checksVoidReturn": {
      "arguments": false
    }
  }]
}

@bradzacher just wanted to let you know I tested in v5.19.0 and it’s still generating the spurious error. I believe this issue needs to be reopened.

I agree, this issue is still persisting.

@bradzacher please review again the OP example which was working in v5.13.0 and broke in v5.14.0+. What’s really weird is that I pasted this same example into the playground page you linked and it is working without error. But when I test this same example in my code using v5.17.0 I get the same linter error as OP. The only difference is that i’m using typescript v4.6.3 and the playground uses typescript v4.6.2.

image
interface Props {
  onEvent: (() => void) | (() => Promise<void>);
}

const Component: React.FC<Props> = () => null;

const App: React.FC = () => {
  const handleEvent = async () => {};
  return <Component onEvent={handleEvent} />;
};

export default App;

Note that onEvent is declared as (() => void) | (() => Promise<void>) and yet it generates the following error when an async function is passed:

9:29  error  Promise-returning function provided to attribute where a void return was expected  @typescript-eslint/no-misused-promises`
image

You’re passing a () => Promise<T> into a position expecting a () => void.

@bradzacher From my reading of this issue I don’t think your explanation is quite correct. In this issue you’re passing a () => Promise<T> into a position expecting a (() => void) | (() => Promise<void>). This means that it’s the calling function’s responsibility to check whether a function returning promise was passed and to handle this case appropriately. But the caller of the function should be free to pass an async function without linter error as the component has indicated it is capable of receiving such a function.