This project supplies the TypeScript types for the WebExtension browser API Polyfill by Mozilla. @types/webextension-polyfill is not manually written, it is generated from these mozilla schema (.json) files:
This used to be a wrapper library, which included webextension-polyfill. If you have been using webextension-polyfill-ts
before, then you can easily move to @types/webextension-polyfill
with these steps:
{ browser }
, you'll now need to import browser
.Examples:
// Before:
import { browser } from "webextension-polyfill-ts";
// After
import browser from "webextension-polyfill";
// Before:
import { browser, Cookies } from "webextension-polyfill-ts";
// After
import browser, { Cookies } from "webextension-polyfill";
This guide assumes you are already using webextension-polyfill. If you are looking for an example use-case, check out the development branch of my web-extension Forget Me Not.
npm install webextension-polyfill
(if you haven't done this already)npm install --save-dev @types/webextension-polyfill
import browser from "webextension-polyfill";
If you want to use the generated types in your code, simply import them like this:
import { Cookies } from "webextension-polyfill";
function inspectCookie(cookie: Cookies.Cookie) {
//...
}
All types are inside their respective namespace:
browser.cookies
types are in the Cookies
namespacebrowser.devtools.inspectedWindow
types are in the DevtoolsInspectedWindow
namespace.If you are loading the polyfill using native ES6 module loader, it will export a global object.
I don't recommend that use-case, but if you are looking for types in this scenario, you can make the types work by creating a file global.d.ts
with this content:
import type Browser from "webextension-polyfill";
declare global {
const browser: Browser.Browser;
}
Note: I haven't tried the ES6 module loader approach, and the above will only get you the types. Check out Basic Setup with module bundlers for more information and don't ask me about it, as I have no clue whatsoever (aside from the types).
Consider mockzilla-webextension for unit-testing. It combines all the types this package provides with some nifty mocking functionality.
There are still some issues left:
See CONTRIBUTING.md
All runtime issues you have should be reported at the webextension-polyfill. If you have problems, questions or other feedback about the generated types, please create an issue here on Github.
My generator code of this project has been released under the zlib/libpng License
The schema files from mozilla however have defined their own licences, which will be exported to the generated @types
project as well.
For example, there are files under the Mozilla Public License and some under a BSD style license.