NN_
NN_

Reputation: 1593

Restrict variable declaration to module

Suppose I write in one module

declare global {
   var SomeVar: {};
}

Now any module can use 'SomeVar' global variable. I would like to restrict the typing only to the current file.

Is it possible ?

Upvotes: 0

Views: 46

Answers (1)

Fenton
Fenton

Reputation: 251262

If you are within a module, you can use:

declare var SomeVar: {};

And SomeVar will only be available within that module/file.

I need to reference something global that is not from my module.

As you can see from the below example, taken from some module, you can use both SomeVarA and SomeVarB within the module.

declare global {
    var SomeVarA: {};
}

declare var SomeVarB: {};

const a = SomeVarA;
const b = SomeVarB;

Both of these declarations can represent a global variable, the only difference is that SomeVarB is not visible to any other modules in your program, whereas SomeVarA is.

For example, in another module the following happens...

// OK
const a = SomeVarA;

// Cannot find name 'SomeVarB'. Did you mean 'SomeVarA'?
const b = SomeVarB;

Upvotes: 1

Related Questions