1

I have a TypeScript project. Basically I have a file called myId.ts:

export module MyModule {
    export class MyId {
        constructor() { ... }
        public compareTo(other: MyId): number { ... }
        public toString(): string { ... }
    }
}

I have a tsconfig.json:

{
    "compilerOptions": {
        "target": "es5",
        "noImplicitAny": true,
        "removeComments": false,
        "preserveConstEnums": true,
        "sourceMap": false,
        "module": "amd",
        "outDir": "./out"
    },
    "files": [
        "myId.ts"
    ]
}

I compile this thing using this command:

tsc --project .

Where the current directory contains the configuration file ad also the source file.

Well, when compiling everything is fine, I get out/myId.js:

define(["require", "exports"], function (require, exports) {
    var MyModule;
    (function (MyModule) {
        var MyId = (function () {
            function MyId() { ... }
            MyId.prototype.compareTo = function (other) { ... };
            MyId.prototype.toString = function () { ... };
        })();
        MyModule.MyId = MyId;
    })(MyModule = exports.MyModule || (exports.MyModule = {}));
});

Problems referencing MyId

Well, I want to use this in a web page and call it from using JavaScript, so by following Require.JS guidelines, I create my web page:

<html>
  <head>
    <meta charset="UTF-8">
    <title>EVT Example - Minimal</title>
    <script src="./require.js" type="text/javascript" data-main="./myId"></script>
  </head>
  <body></body>
</html>

However, when I open the inspector (F12 tools) and try referencing MyModule.MyId, nothing can be found! How can I reference my type?

Everything loaded fine

The inspector told me that myId.js has been successfully loaded actually. So it is not a problem concerning the fact that Require.JS did not load the script.

What to do?

1 Answer 1

2

when I open the inspector (F12 tools) and try referencing MyModule.MyId, nothing can be found! How can I reference my type

Modules are not global and that is a good thing. So if you type MyModule.MyId nothing will happen.

If you want to access it on the console you can put it on window e.g.:

export module MyModule {
    export class MyId {
        constructor() { ... }
        public compareTo(other: MyId): number { ... }
        public toString(): string { ... }
    }
}
(window as any).MyModule = MyModule;

Then you can do MyModule.MyId in the console. Of course there is only one global space and its an awful idea to start polluting it (kills the purpose of modular maintainable JavaScript). Its okay to do it for testing 🌹

Sign up to request clarification or add additional context in comments.

4 Comments

I see... That is nice. However I have a question... Where does actually require.js put all the stuff? If not in the window object, where?
In a function wrapped by a define call. The top level define is called by data-main 🌹
Is as allowed in TypeScript?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.