Skip to content

Multi-file non-public module variables #1556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Dec 24, 2014 · 2 comments
Closed

Multi-file non-public module variables #1556

ghost opened this issue Dec 24, 2014 · 2 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@ghost
Copy link

ghost commented Dec 24, 2014

Hello, is it intended behavior for this to happen:

//FILE 1
module Test {
    var x : WebGLRenderingContext;

    export class Test1 {
        //Assume x is initialized
    }
}
//FILE 2
module Test {
    export class Test2 {
        //Assume member method clearColor() is called on x
        //Error: Cannot find name 'x'
    }
}

which compiles to:

//FILE 1
var Test;
(function (Test) {
    var x;
    var Test1 = (function () {
        function Test1() {
        }
        return Test1;
    })();
    Test.Test1 = Test1;
})(Test || (Test = {}));
//FILE 2
var Test;
(function (Test) {
    var Test2 = (function () {
        function Test2() {
        }
        return Test2;
    })();
    Test.Test2 = Test2;
})(Test || (Test = {}));

Note that this is a module that takes place in two separate files. I am attempting to access a method of x from a file where x was not specifically declared, but it was declared for the module.

If you combine File 1 and File 2 to get something like this:

//FILE 1
module Test {
    var x : WebGLRenderingContext;

    export class Test1 {
        //Assume x is initialized
    }

    export class Test2 {
        //Assume member method clearColor() is called on x
        //Works fine
    }
}

which compiles to:

//FILE 1
var Test;
(function (Test) {
    var x;
    var Test1 = (function () {
        function Test1() {
        }
        return Test1;
    })();
    Test.Test1 = Test1;
    var Test2 = (function () {
        function Test2() {
        }
        return Test2;
    })();
    Test.Test2 = Test2;
})(Test || (Test = {}));

everything works fine. The only solution I have found was to export the variable in the first file. However, the reason I'm not exporting it already is because I don't want the variable to visible outside the module.

Could someone explain why two modules in separate files wouldn't be merged?

@wasker
Copy link

wasker commented Jan 3, 2015

I worked it around by adding a definitions.d.ts with
declare module Test {
var x: WebGLRenderingContext;
}

And then including this file in both FILE 1 and FILE 2 as a and removing x declaration from both FILE 1 and FILE 2.

@danquirk
Copy link
Member

danquirk commented Jan 5, 2015

Yes this is by design. The reason is because in the first case the only way to make it work the way you want would be to reorder the emitted JS that corresponds to the TS you wrote. This is something we generally try to avoid doing for a few reasons. More problematic than any philosophical concerns about rewriting emit is the difficulty in actually doing this rewriting correctly. There're all kinds of code patterns you could've used between these two modules which are far more complicated than the case you have here (which is essentially the simplest possible case). Your workaround is the only real solution right now, but what you probably want is an internal kind of modifier like #892

@danquirk danquirk closed this as completed Jan 5, 2015
@danquirk danquirk added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Jan 5, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

2 participants