retorquere
retorquere

Reputation: 1566

Declaring an variable initialized in another file

I have some code that references a variable I know has already been declared in a file loaded before mine as in

if (!Zotero.BetterBibTeX) { ... }

but this gets me "Cannot find name 'Zotero'". Is there a way to signal to the typescript compiler that "Zotero" is declared?

Upvotes: 1

Views: 429

Answers (2)

Pape
Pape

Reputation: 291

Well you need to import Zotero into your module. Declaring it as any does solve the compile error.. but that's not really the TypeScript way of importing modules.

// -------------------
// File ./zotero.ts
// -------------------
export class Zotero {
  someFunction() {
    // some code..
  }  
}

// -------------------
// File ./main.ts
// -------------------
import { Zotero } from "./zotero.ts";

// Now you can use Zotero.
let z = new Zotero();
z.someFunction();

Upvotes: 0

James Monger
James Monger

Reputation: 10685

You can add this at the top of the file where you're using Zotero:

declare let Zotero: {
    BetterBibTeX: any;
};

Then you can use if (!Zotero.BetterBibTeX) { ... } as you like.


If you don't want to have any type checking around the properties on Zotero, you can just declare it as an any type:

declare let Zotero: any;

Upvotes: 2

Related Questions