[@types/react] <Form onSubmit> should allow an async event handler
#66505
-
|
React allows passing an async event handler for
While TLDR: [@types/react] should allow async event handlers |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 23 replies
-
|
Thanks for the discussion about "react", some useful links for everyone: Pinging the DT module owners: @johnnyreilly, @bbenezech, @pzavolinsky, @ericanderson, @DovydasNavickas, @theruther4d, @guilhermehubner, @ferdaber, @jrakotoharisoa, @pascaloliv, @Hotell, @franklixuefei, @Jessidhia, @saranshkataria, @lukyth, @eps1lon, @zieka, @dancerphil, @dimitropoulos, @disjukr, @vhfmag, @hellatan, @priyanshurav, @Semigradsky. |
Beta Was this translation helpful? Give feedback.
-
|
@eps1lon what are your thoughts here on changing the React types to also accept async functions for event handlers? Here's an example of an error from the recommended configuration of
import 'react';
export default function Component() {
return <button onClick={async () => 0} />;
}Error message: WorkaroundDisable the lint warnings/errors about promise misuse reported using
'@typescript-eslint/no-misused-promises': [
'error',
{
checksVoidReturn: {
// Allow async functions passed to event handler props
// - https://github.com/typescript-eslint/typescript-eslint/pull/4623
// - https://github.com/typescript-eslint/typescript-eslint/issues/4619
//
// Although technically, async functions should not
// be passed to event handler props:
// - https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/66505#discussioncomment-10411110
attributes: false,
},
},
], |
Beta Was this translation helpful? Give feedback.

We've discussed this and we don't want to allow async functions at the type level. We're probably going to start warning at runtime when you return a Promise from an event handler.
We didn't used to warn because we didn't have a good alternative. With React 19 we have: Use an Action or wrap the async function in
startTransition.We may start automatically wrapping async functions in
startTransition. But with any additive API this comes with the risk of breaking existing usages that relied on existing behavior even though it was never supported. So by officially allowing them now (even though it "just works"), we increase the risk of not being able to make that change because too much of t…