Currently, I am in the process of converting an existing AngularJS (1.x) project to TypeScript. The code seems to be functioning properly, but there are some aspects that leave me with questions.
In JavaScript, I declare a module like this:
(function() {
'use strict';
angular.module('myModule', []);
})();
In TypeScript, the declaration looks like this:
module myModule {
'use strict';
angular.module('myModule', []);
}
This is then compiled to:
var myModule;
(function (myModule) {
"use strict";
angular.module("myModule", []);
})(myModule || (myModule = {}));
I have a few uncertainties:
- Is it incorrect to introduce
var myModule
into the global scope? - Why is
myModule
passed into the anonymous function? - Considering the addition of
var myModule
, should I avoid overwriting it when declaring other components such as Controllers? Would using a differentmodule
name be more appropriate here?
In essence, can the same name be reused for another module, like so:
module myModule {
angular
.module('myModule')
.controller('myController');
}
// potentially overwriting the existing var myModule
Alternatively, would it be wiser to utilize a separate name for the new module, for instance:
module myModuleController {
angular
.module('myModule')
.controller('myController');
}
// generating a distinct var myModule
The functionality appears unaffected regardless, but I believe that there may be important considerations regarding this matter that I should be aware of.