Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript package management

Tags:

typescript

Looks like TypeScript has a nice module system, however does this replace the need for something like requirejs? In other words, when you "compile" a typescript package does it handle all the dependency resolution for you? Examples would be appreciated!

like image 802
Abdullah Jibaly Avatar asked Oct 03 '12 18:10

Abdullah Jibaly


1 Answers

TypeScript does not have a runtime module loader. You will still need to provide a module loader to use at runtime, e.g. require js. TypeScript supports generating JavaScript code compatable with either commonJS (for node.js scripts) and AMD loaders (e.g. requireJS). To specify which one to use pass in the "--module " switch to the compiler with either "amd" or "commonjs".

Here is how you export a module in TypeScript:

export module depModule { 
    export class A { 
    }
}

and here is the generated JavaScript code with --module amd switch:

define(["require", "exports"], function(require, exports) {
    (function (depModule) {
        var A = (function () {
            function A() { }
            return A;
        })();
        depModule.A = A;
    })(exports.depModule || (exports.depModule = {}));
})
like image 170
mohamed hegazy Avatar answered Oct 31 '22 00:10

mohamed hegazy