Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update runtime/tabs.sendMessage types to include callback #64

Open
mp3por opened this issue Jan 4, 2024 · 3 comments
Open

Update runtime/tabs.sendMessage types to include callback #64

mp3por opened this issue Jan 4, 2024 · 3 comments

Comments

@mp3por
Copy link

mp3por commented Jan 4, 2024

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.

image
@yellott
Copy link

yellott commented Feb 22, 2024

@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.

image

@mp3por
Copy link
Author

mp3por commented Mar 1, 2024

@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.

image

I did not understand your EDIT. How to reference the correct type ?

@yellott
Copy link

yellott commented Mar 1, 2024

@mp3por Sorry, I didn't explain clearly. So if you don't need/want to use Promise version of sendMessage you have two options:

  1. 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);
});
  1. Since the documentation states that the options parameter 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants