0

I have a component, that has onClick prop.

I'm passing it an async handler, and getting the error:

Type '() => Promise' is not assignable to type 'EventHandler<ClickEvent, boolean | void>'. Type 'Promise' is not assignable to type 'boolean | void'. Type 'Promise' is not assignable to type 'void'.ts(2322)

Code

onClick={async () => {
        await item.onClick();
        menuRenderParams.closeMenu();
      }}

I tried using named handler _onClick, and specify the return type, but it didn't work.

While using the named handler I tried give the handler type like this: _onClick: EventHandler but got this error:

Generic type 'EventHandler' requires 1 type argument(s).

I look around for typescript promise handler type but didn't found anything.

  • side note, I added "strict": false to the .tsconfig, but the compilation still fails. if you could help me understand this also it would be gr8.
0

1 Answer 1

0

This is gonna work fine i think your type is incorrect.

take a look at this code:

type SomeFunc = () => Promise<string>

const func: SomeFunc = () => {
  return new Promise<string>((resolve, reject) => resolve("someinfo"));
};


<div
  onClick={async (event: MouseEvent) => {
    event.preventDefault();

    const result = await func();
    console.log(result);
  }}
></div>

it works fine please share your type here. make sure you have imported MouseEvent type from react

Sign up to request clarification or add additional context in comments.

2 Comments

getting this error now: Type '(event: MouseEvent) => Promise<void>' is not assignable to type 'EventHandler<ClickEvent, boolean | void>'.
stackoverflow.com/users/9608006/itay-tur Show me your component and type file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.