TypeScript compile AMD modules with necessary definitions

In AMD (as implemented in requirejs), modules can be defined as dependencies, for example:

define(['require','exports'], function(require, exports) { var externalDep = require('path/to/depModule'); // Use the module somewhere. }); 

I tried --module amd and correctly output the AMD module, which can be used with requirejs.

Is it possible to determine the dependencies inside the source of the TypeScript source file, which translates somehow to the example above?

+8
requirejs typescript
source share
1 answer

You need to "export" your modules;

 export module depModule { export class A { } } 

which will be translated into JavaScript code that looks like this:

 define(["require", "exports"], function(require, exports) { (function (depModule) { var A = (function () { function A() { } return A; })(); depModule.A = A; })(exports.depModule || (exports.depModule = {})); }) 

and then you use them with "import":

 module otherModule { import depModule = module('depModule'); var a = new depModule.depModule.A(); } 

you will need to specify the type of code generation of your module in the compiler using --module AMD.

+13
source share

All Articles