chrome-types
chrome-types copied to clipboard
`sendResponse`, the 3rd param of `addListener` callback function accepts expected 1 param in `any` type but 0 now
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.
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.
I think someone who works at Google will have to fix it.
I have fixed this localy using declaration merging. Hope this helps someone.
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
>;
}
}
It is recommended to try the @types/chrome
type library, as it has more comprehensive type definitions and fewer errors.