Below is the TypeScript class found in the file called bar.ts
export class Bar {
log(message:string):void {
console.log(message);
}
}
In a separate file named foo.ts, the following code exists:
import { Bar } from "bar";
window.onload = () => {
var foo = new Bar();
foo.log("Hello World");
};
During compilation, the settings are as follows:
lib: ["dom", "es2015.promise", "es2015"]
module: "amd"
declarationFiles: true
target: "ES5"
noImplicitAny: true
out: "test.js
What is the correct way to create an instance of the Bar class? The window.onload event does not seem to trigger. Here is the resulting JavaScript code:
define("bar", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Bar = (function () {
function Bar() {
}
Bar.prototype.log = function (message) {
console.log(message);
};
return Bar;
}());
exports.Bar = Bar;
});
define("foo", ["require", "exports", "bar"], function (require, exports, bar_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
window.onload = function () {
var foo = new bar_1.Bar();
foo.log("Hello World");
};
});
Is there a method to access Bar from regular JavaScript? I have included require.js in the HTML page and no errors occur when loading the page.
This scenario is a simplified version, but it is necessary to use export due to multiple classes stored in individual files that need to be merged into one.