GoogleChrome / chrome-types

Code to parse Chrome's internal extension type definitions—published on NPM as chrome-types

Home Page:https://www.npmjs.com/package/chrome-types

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

YuanSa opened this issue · 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
commented

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

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

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.

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.

commented

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

commented

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