Update runtime/tabs.sendMessage types to include callback
As per documentation of runtime.sendMessage and tabs.sendMessage the methods have a 4th parameter callback. At the moment this parameter is missing from the types and typescript complains.
@mp3por temp solution described here
UPD: I haven't noticed that you referencing Promise based version. So this one is correct, there's also overload with callback version.
@mp3por temp solution described here
UPD: I haven't noticed that you referencing Promise based version. So this one is correct, there's also overload with callback version.
I did not understand your EDIT. How to reference the correct type ?
@mp3por Sorry, I didn't explain clearly. So if you don't need/want to use Promise version of sendMessage you have two options:
- Explicitly pull desired type from provided type definitions by matching the overload, like so:
chrome.runtime.sendMessage({ type: "MessageType" }, undefined, (response) => {
console.log("response", response);
});
- Since the documentation states that the
optionsparameter is optional (yet there is no overload for cases where the callback is the second/third parameter, even though it is entirely legal according to my tests), you can extend the default types so that the callback can be passed as the second or third parameter, like so:
// Path: src/global.d.ts
declare namespace chrome {
/// <reference types="chrome-types" />
export namespace runtime {
export function sendMessage(
extensionId: string,
message: any,
callback?: (response: any) => void,
): void;
export function sendMessage(
message: any,
callback?: (response: any) => void,
): void;
}
}
UPD: After adjusting types you'll be able to call sendMessage like this:
chrome.runtime.sendMessage({ type: "MessageType" }, (response) => {
console.log("response", response);
});
For simplicity I've only showed example for chrome.runtime.sendMessage but the same can be applied to chrome.tabs.sendMessage.
Let me know if this is clear or any further help is required.
