GoogleChrome / chrome-types

Code to parse Chrome's internal extension type definitions—published on NPM as chrome-types
https://www.npmjs.com/package/chrome-types
Apache License 2.0
189 stars 30 forks source link

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

Open YuanSa opened 1 year ago

YuanSa commented 1 year ago

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 commented 7 months ago

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

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

mattmcnally commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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

yellott commented 7 months ago

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 commented 1 month ago

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