Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sendResponse, the 3rd param of addListener callback function accepts expected 1 param in any type but 0 now #50

Open
YuanSa opened this issue May 28, 2023 · 6 comments

Comments

@YuanSa
Copy link

YuanSa commented May 28, 2023

As I expected, the sendResponse callback should accept 1 param in any type but now 0.

Thus the following codes will fail to pass the type checking.

Screenshot 2023-05-28 at 20 23 31
@rob4226
Copy link

rob4226 commented Feb 11, 2024

The type should be something like sendResponse: (response?: any) => void

The types are generated automatically so how do we fix this?

@mattmcnally
Copy link

This appears to affect not just runtime.onMessage but also runtime.onMessageExternal and runtime.onUserScriptMessage. The invalid definition originates from Chrome's documentation: https://developer.chrome.com/docs/extensions/reference/api/runtime#event-onMessage - an override might be needed here to enforce inclusion of a response argument.

@amkhrjee
Copy link

According to MDN:

The function takes a single argument, which may be any serializable object

So sendResponse(response: any) => void should do the trick right?

And why isn't this issue fixed yet? This seems like a longstanding problem.

@rob4226
Copy link

rob4226 commented Feb 18, 2024

I think someone who works at Google will have to fix it.

@yellott
Copy link

yellott commented Feb 20, 2024

I have fixed this localy using declaration merging. Hope this helps someone.

image

declare namespace chrome {
  /// <reference types="chrome-types" />
  export namespace runtime {
    export const onMessage: events.Event<
      (
        message: any,
        sender: MessageSender,
        sendResponse: (data: any) => void,
      ) => boolean | undefined
    >;
  }
}

@ouweiya
Copy link

ouweiya commented Sep 4, 2024

It is recommended to try the @types/chrome type library, as it has more comprehensive type definitions and fewer errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants