Lusito / webextension-polyfill-ts

This is a TypeScript ready "wrapper" for the WebExtension browser API Polyfill by Mozilla
zlib License
392 stars 23 forks source link

Fix global type documentation #71

Closed green1052 closed 2 years ago

green1052 commented 2 years ago

Browser.Browser is invalid

Lusito commented 2 years ago

Thanks for taking the time for a PR, but I don't think this is correct. Browser is a namespace containing an interface named Browser. So you'd want the interface by specifying Browser.Browser.

Browser.Browser reads a bit confusing, I agree, but still the readme is correct here.. Feel free to argue against this with something I can reproduce.

Lusito commented 2 years ago

Nevermind, I see the problem now. There is another issue with the sample code though: Circular definition of import alias 'Browser' on line 1.

What seems to work for both cases on my end is this:

import type Browser from "webextension-polyfill";

declare global {
    const browser: Browser.Browser;
}

Can you verify this works for you as well?

fregante commented 2 years ago

The types export both a Browser namespace as default and a Browser interface as a named export:

https://user-images.githubusercontent.com/1402241/158057495-286790b3-409e-48d2-bd0c-78c3f7d67ebd.mov

Your intent was probably import type Browser but wrote import type { Browser.

Either way works:

import type Browser from "webextension-polyfill";

declare global {
    const browser: Browser.Browser;
}
import type { Browser } from "webextension-polyfill";

declare global {
    const browser: Browser;
}

Given that the latter is shorter, that's what I prefer

Lusito commented 2 years ago

This is what I get when I try your approach: Screenshot from 2022-03-13 12-42-17

Not sure if it has to do with me using that in a project not properly set up for ES6 module loader.

fregante commented 2 years ago

That's likely because you're adding that file to this project 😅

Lusito commented 2 years ago

No, I added it to https://github.com/Lusito/dict.cc-translation