Skip to content

Separated internal module to a single function #4779

@apastuhov

Description

@apastuhov

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'.
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions