Skip to content
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

Separated internal module to a single function #4779

Closed
apastuhov opened this issue Sep 14, 2015 · 4 comments
Closed

Separated internal module to a single function #4779

apastuhov opened this issue Sep 14, 2015 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@apastuhov
Copy link

Why TS generates two functions, but not one?

We have one module separated between two files in TS, or in one file, but separated by other code. And we need to call F1 from F2. But do not need to cal F1 outside of Main module. Now we can write just like this:

module Main {
    export function F1() {
    }
}
module Main {
    export function F2() {
        F1();
    }
}

And it compiles to JS as two independent functions, so user can access F1 function:

var Main;
(function (Main) {
    function F1() {
    }
    Main.F1 = F1;
})(Main || (Main = {}));
var Main;
(function (Main) {
    function F2() {
        Main.F1();
    }
    Main.F2 = F2;
})(Main || (Main = {}));

But it seems to me that it will be better to concatenate the content of the modules with single name to one function. Like this JS output:

var Main;
(function (Main) {
    function F1() {
    }
    function F2() {
       F1();
    }
    Main.F2 = F2;
})(Main || (Main = {}));

So there will be no need to create additional export members just to allow execute one members from the others inside one module.

p.s. Such code will fail:

module Main {
    function F1() {
    }
}
module Main {
    export function F2() {
        F1(); // error TS2304: Cannot find name 'F1'.
    }
}
@apastuhov
Copy link
Author

I know that I can write:

module Main {
    function F1() {
    }
    export function F2() {
        F1();
    }
}

But it is not a good idea, because sometimes I have very big classes and functions.

@danquirk
Copy link
Member

The problem is that it is not generally safe to merge module/namespace declarations like this. We would have to figure out if/when it's safe and only merge then (or does the user need to specify it?). We'd like also need #892. In any case, there is another suggestion already logged to track this idea but I can't find it at the moment. Someone wrote a node package that can perform the optimization for you but it is up to you to ensure the optimization is safe for your codebase.

@apastuhov
Copy link
Author

@danquirk Yes, user need to specify. I found that suggestion after I created this one. It was created 10 days ago #4652. You are right, there is an NPM package which will concatenate the output.. but IDE(VSCode, VS2015 or other) will message me an error(read P.S. of the issue). internal is good idea. and probably what I want as result.

@danquirk
Copy link
Member

Thanks for finding that, I knew it was just the other day but search was failing me. I'm going to close this issue as a dupe, feel free to add your support/ideas to either of those suggestions.

@danquirk danquirk added the Duplicate An existing issue was already created label Sep 14, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants