nope
nope

Reputation: 1078

How can I make code generated by TypeScript using AMD modules, actually execute?

I just started using TypeScript and I made 2 files, lib.ts and main.ts, and picked AMD modules during compilation. The compiler generates one file with definitions but it doesn't actually call any function to execute. The contents of the main.ts file are just wrapped inside a module. Here are the files:

lib.ts

export namespace Library {
    export class Write {
        constructor() {
            console.log("hello world");
        }
    }
}

main.ts

import {Library} from "./lib";
new Library.Write();

The compiler command:

tsc main.ts --outFile out.js --module amd

out.js

define("lib", ["require", "exports"], function (require, exports) {
    "use strict";
    var Library;
    (function (Library) {
        var Write = (function () {
            function Write() {
                console.log("hello world");
            }
            return Write;
        }());
        Library.Write = Write;
    })(Library = exports.Library || (exports.Library = {}));
});
define("main", ["require", "exports", "lib"], function (require, exports, lib_1) {
    "use strict";
    new lib_1.Library.Write();
});

How can I get the compiler to actually generate code that executes? The documentation does not help.

Upvotes: 1

Views: 490

Answers (1)

Paleo
Paleo

Reputation: 23692

The option --outFile should not be used. The AMD format must be used with the asynchronous loader RequireJS, in browsers. RequireJS will load the multiple generated JavaScript files on demand.

Also, you need to load the RequireJS runtime in the browser. Cf the documentation.

Notice: About the code export namespace, it is not recommended to mix namespaces and modules, see this answer.

Upvotes: 2

Related Questions