TypeScript modules TypeScript modules typescript typescript

TypeScript modules


This is exactly how it works! If you look at the generated javascript code, it add as an anonymous function that accepts an object, the "the module object":

var mylib;(function (mylib) {    var Button = (function () {        function Button(x) {            this.x = x;        }        return Button;    })();    mylib.Button = Button;    })(mylib || (mylib = {}));

If you look at the last line (})(mylib || (mylib = {}));) you see that it instantiates a new ojbect (mylib = {}) only if the existing variable is false (or something that evaluates to false, like null). That way, all "modules" that are named the same will be merged to the same object.

Therefore, internal modules extend each other. I have to note that I have not quite figured out what happens to nested modules.

Update: Your code works for me if I do not use the nested module syntax, but change it to the dot syntax. e.g.:

module mylib.gui {}

instead of

module mylib {    module gui {    }}

I'll try to investigate in why this is happening, as far as I have read the spec, both ways should be equal.

Update: if the nested referenced module is marked as exported, it works:

module mylib {    export module gui {    }}