Reputation: 1087
I am new to typescript (and javascript for that matter). I put together a tiny test to see if I can do the following based on reading up on AMD, looking at JS based requires and looking at the TS doc.
Test.ts
export class TestClass {
constructor() {
}
testMethod(): void {
}
}
export function Singleton(): void {
}
Foo.ts
import Test = require('Test');
export class Bar {
private instance = new Test.TestClass();
private singleton = Test.Singleton;
constructor() {
this.instance.testMethod();
this.singleton();
}
}
Generated JS
// Foo.js
define(["require", "exports", 'Test'], function(require, exports, __Test__) {
var Test = __Test__;
var Bar = (function () {
function Bar() {
this.instance = new Test.TestClass();
this.singleton = Test.Singleton;
this.instance.testMethod();
this.singleton();
}
return Bar;
})();
exports.Bar = Bar;
});
// Test.js
define(["require", "exports"], function(require, exports) {
var TestClass = (function () {
function TestClass() {
}
TestClass.prototype.testMethod = function () {
};
return TestClass;
})();
exports.TestClass = TestClass;
function Singleton() {
}
exports.Singleton = Singleton;
});
Based on my code above, is my list of what I wanted to do correct? If so YAY, if not I think I may have missunderstood something about AMD :(
Upvotes: 1
Views: 207
Reputation: 276057
Your understanding of AMD is immaculate. However your understanding of the singleton pattern is incorrect. Have a look at this for an example : http://www.codebelt.com/typescript/typescript-singleton-pattern/
Upvotes: 2