Flion
Flion

Reputation: 10902

Do I need to declare a global variable inside every external typescript module?

I've been using one global variable called system which is defined in index.ts. When I was using internal modules that went fine, probably because I started compiling in index.ts with --out.

Now I'm switching to external modules the compiler throws errors for the global variable 'system'. I kept a single in each file with some .d.ts files for external libs, and I tried adding

declare var system:System

in that shared reference file, but that didnt work.

What does work is adding the declare statement to each file that uses the global variable. So my question is if this is the way I should do it (declaring in every file), or if there's something I'm missing.

Tnx!

Upvotes: 0

Views: 1978

Answers (1)

Fenton
Fenton

Reputation: 250972

In Visual Studio 2013 (Update 3) the mere presence of system.d.ts is enough in the test I set up...

system.d.ts (I made this up)

interface System {
    someStuff(): void;
}

declare var system: System; 

afile.ts

class Lower {

    constructor(private word: string) {
        system.someStuff();
    }
}

export = Lower 

And I could access system.someStuff(); from anywhere.

If you are using a different IDE, you may need to add:

///<reference path="system.d.ts" />

This hints to the compiler that the definition exists, but doesn't actually import system as an external module (you can use import system = require('system'); if you want to load it like a module, but I don't think that's what you want in this case as you've stated that this is a global variable.

Upvotes: 1

Related Questions