admin管理员组

文章数量:1290306

I am creating a WebExtension using TypeScript which is later piled to JavaScript.

My extension depends on one of the APIs the browser (Firefox) offers, specifically the extension API. As an example, I use the getURL() method, which is called like this:

browser.extension.getURL("foo/bar.js");

Of course, TypeScript gives an error "Cannot find name 'browser'". This prevents me from fully piling the code. I would like to know if there is any way to bypass this. Preferably not only at pile level, but also at the linting level.

I have tried:

  • Defining browser at the beginning as var browser: any;: breaks the API.
  • Compiling with --noEmit, --noEmitOnErrors: irrelevant, still plains.

Any suggestions?

I am creating a WebExtension using TypeScript which is later piled to JavaScript.

My extension depends on one of the APIs the browser (Firefox) offers, specifically the extension API. As an example, I use the getURL() method, which is called like this:

browser.extension.getURL("foo/bar.js");

Of course, TypeScript gives an error "Cannot find name 'browser'". This prevents me from fully piling the code. I would like to know if there is any way to bypass this. Preferably not only at pile level, but also at the linting level.

I have tried:

  • Defining browser at the beginning as var browser: any;: breaks the API.
  • Compiling with --noEmit, --noEmitOnErrors: irrelevant, still plains.

Any suggestions?

Share Improve this question asked Jul 26, 2018 at 11:34 user8774937user8774937 1831 silver badge7 bronze badges 2
  • You can also try browser['extension']['getURL']("foo/bar.js"); – Shivam Muttoo Commented Jul 26, 2018 at 11:38
  • @mpm Apparently a var between declare and browser is needed, as @TitianCernicova-Dragomir said. Thank you anyway :) – user8774937 Commented Jul 26, 2018 at 11:41
Add a ment  | 

1 Answer 1

Reset to default 12

If you want to let Typescript know that the variable exists but not actually emit any code for it you can use declare

declare var browser: any;

本文标签: javascriptIgnore not declared variables (TypeScript)Stack Overflow