chrome-types icon indicating copy to clipboard operation
chrome-types copied to clipboard

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

Open YuanSa opened this issue 1 year ago • 6 comments

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

YuanSa avatar May 28 '23 12:05 YuanSa

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

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

rob4226 avatar Feb 11 '24 09:02 rob4226

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.

mattmcnally avatar Feb 17 '24 13:02 mattmcnally

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.

amkhrjee avatar Feb 18 '24 11:02 amkhrjee

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

rob4226 avatar Feb 18 '24 11:02 rob4226

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
    >;
  }
}

yellott avatar Feb 20 '24 13:02 yellott

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

ouweiya avatar Sep 04 '24 20:09 ouweiya