chromeExtensionAsync
chromeExtensionAsync copied to clipboard
Add a working Typescript example
I spent half a day trying to get a working TypeScript extension. No matter what I do, I can't get chrome to recognize the promisified interface (Typescript does, but Chrome doesn't). I'm sure I'm just missing one or two lines from tsconfig.json. I'm also sure I'm not the only one facing this issue.
I'm assuming you mean that, when the extension is run in Chrome, undefined
is returned by the chrome functions? (that is, they only use the callback instead of returning a promise)
If so, did you remember to include this line before your chrome-API-accessing code runs?
import "chrome-extension-async";
// or
require("chrome-extension-async");
Currently the definition files declare a namespace, which works with TS's default no modules pattern.
I should update them to use ES6 modules, adding an export default
to the definition and requiring an import chrome from 'chrome-extension-async'
in your file, but that may be a breaking change to users of the previous TS model.
PRs with fixes welcome :)
I did not add that import, which is obviously stupid omission on my part. As I said in my original post - it must be one or two missing lines (although not in tsconfig). I don't think you need to change the code at all, just provide an example.
An example is going to depend on the module model you're using.
For JS:
// @ts-check
/// <reference path="./node_modules/chrome-extension-async/chrome-extension-async.d.ts" />
For TS:
import './node_modules/chrome-extension-async/chrome-extension-async.js';
Should automatically pick up the definition, but it will depend on how you have configured TS. If you're using global references (i.e. all TS compiles to one big JS output) then use the reference comment that you would for @ts-check
.
import './node_modules/chrome-extension-async/chrome-extension-async.js';
maybe put the above on the readme ...? I struggled with this as well; found this and just starting to use it for the first time after struggling for 20 minutes. as I have not yet used a typescript node module without types declared as such
Update
Struggled for 30 minutes due to incompatibilities as ts compilation issues in webpack with @types/chrome then found this fork: https://www.npmjs.com/package/chromep
..and a npm module which uses that:
https://www.npmjs.com/package/chrome-promise like
import chromep from 'chrome-promise';
Why does import './node_modules/chrome-extension-async/chrome-extension-async.js';
make it work? I want to make a PR to fix this but I don't understand it lol.