Lusito/webextension-polyfill-ts

Potentially incorrect type using browser.management.onUninstalled

kakajuro opened this issue · 3 comments

Hi there, I think there may be an incorrect type on parameter for the browser.management.onUninstalled api. When I use the addListener method, the type is defined as a Management.ExtensionInfo type, however the parameter actually only returns a string which is the id of the uninstalled extension.

This is the code I am trying to run and if I were to use it in the intended way using the value info.id, it comes back undefined when I try to use it.

browser.management.onUninstalled.addListener(info => {
  if (info.id === browser.runtime.id) {
    // Code continues here
  }
});

Since the parameter returns the id of the uninstalled extension I tried running the following code block but ran into this error because of the mismatched types:

This comparison appears to be unintentional because the types 'ExtensionInfo' and 'string' have no overlap .ts(2367)

browser.management.onUninstalled.addListener(id => {
  if (id === browser.runtime.id) {
    // Code continues here
  }
});

The equivalent Chrome api parameter is typed as a string so I think existing type definition is incorrect.
image

Interesting, you seem to have found a difference between the chrome API and the webextension API:

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/management/onUninstalled
https://developer.chrome.com/docs/extensions/reference/api/management

This seems like a problem for the polyfill itself though, since this project is only about the types.

Please try to open an issue there to see if they want to fix it. If they won't, I can only offer to make the type a string | ExtensionInfo and you'll have to check the type in the listener, since chrome supplies a string and firefox would supply an object. Not sure about other browsers.

For a quick fix, the following might also work, even though it's not entirely accurate without adjusting the types in this project:

browser.management.onUninstalled.addListener((info) => {
    const id = typeof info === "string" ? info : info.id;

Seems like this is just a difference between the browser APIs themselves then. I will definitely try to open an issue over on the main pollyfill repo. I guess alot of these more niche APIs are used much more infrequently than say the storage API so inconsistencies like this can go under the radar quite easily. Since I made this issue initially I ended up not using this API anyways but I appreciate the quick fix regardless. Thanks for the help!