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)
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:
@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.
Note that
onEventis declared as(() => void) | (() => Promise<void>)and yet it generates the following error when an async function is passed:@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.