I am using an Angular application to use TypeScript, but this is a general TypeScript question, not Angular. Angular js files are located along the lines:
(function () { var app = angular.module('myModule', []); app.controller('myController', ['$scope', function ($scope) { $scope.myNewProperty = "Bob"; } ]); })();
And I converted this to a beautiful TypeScript class syntax:
class myController { constructor($scope) { $scope.myNewProperty = "Bob"; } } angular.module('myModule', []).controller("myController", myController);
Everything works fine, except that the generated JS is not wrapped according to the JS module template (i.e. in an external anonymous function):
var myController = (function () { app.controller('myController', ['$scope', function ($scope) { $scope.myNewProperty = "Bob"; } ]); })(); var app = angular.module('myModule', []);
So myController is now global. If I put the class in a TypeScript module, then js generates the module name as a global variable:
var TsModule; (function (TsModule) { var myController = (function () { app.controller('myController', ['$scope', function ($scope) { $scope.myNewProperty = "Bob"; } ]); })(); var app = angular.module('myModule', []); })(TsModule || (TsModule = {}));
How to stop TypeScript global area pollution this way? I just want it all to be on a good local scale. I saw him say elsewhere: "just go back to the old JS syntax" (i.e. TypeScript class). How to define an AngularJS service using a TypeScript class that does not pollute the global scope?
But we all use TypeScript / is / class syntax. Is TypeScript a disagreement with any (sensible) JS programmer who knows that he is not global?