1

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?

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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();

2 Comments

The code that declares Zotero is not my own (or written in TS); I write an extension that loads after that code has run.
Ah i see. You can import js-files as well into your TypeScript and might need to write a d.ts file for it. (Or be lazy and declare to any as mentioned by James)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.