Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore not declared variables (TypeScript)

I am creating a WebExtension using TypeScript which is later compiled 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 compiling the code. I would like to know if there is any way to bypass this. Preferably not only at compile 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 complains.

Any suggestions?

like image 526
user8774937 Avatar asked Sep 08 '25 01:09

user8774937


1 Answers

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;
like image 54
Titian Cernicova-Dragomir Avatar answered Sep 10 '25 05:09

Titian Cernicova-Dragomir