How can I define dependent modules in TypeScript AMD Modules

后端 未结 2 1721
眼角桃花
眼角桃花 2021-02-04 18:34

How can I define the dependent modules in a AMD Module with TypeScript, including, for instance, jQuery and underscore. Using JavaScript I do, for example, like this:

         


        
2条回答
  •  终归单人心
    2021-02-04 19:14

    With the current implementation of the compiler (0.8) the way to achieve what you are seeking is the following.

    test.ts:

    import jquery = module("jquery");
    import underscore = module("underscore");
    
    export module A {
        console.log(jquery.$ + underscore._);
    }
    

    jquery.ts:

    export var $ = null;
    

    underscore.ts:

    export var _ = null;
    

    if you compile test.ts using the module switch:

    tsc --module AMD test.ts
    

    it will generate the following JS file for you:

    define(["require", "exports", "jquery", "underscore"], function(require, exports, __jquery__, __underscore__) {
        var jquery = __jquery__;
        var underscore = __underscore__;
        (function (A) {
            console.log(jquery.$ + underscore._);
        })(exports.A || (exports.A = {}));
    })
    

    Note that if you put import statements inside the module it will note codegen correctly due to a bug. The team is currently working on a fix on that item but hopefully that shouldn't block you.

提交回复
热议问题