I faced an issue when using the methodology of one file - one class in a TypeScript project, as shown in the example below.
File Greeter.ts
module App {
export class Greeter {
constructor(public greeting: string) { }
public greet() {
return "<h1>" + this.greeting + "</h1>";
}
};
}
File Program.ts
module App {
export class Program {
private _greeter = new Greeter("Hello, world!");
public main() {
document.body.innerHTML = this._greeter.greet();
}
};
}
Result file
var App;
(function (App) {
var Greeter = (function () {
// class code
} ());
App.Greeter = Greeter;
})(App || (App = {}));
var App;
(function (App) {
var Program = (function () {
// class code
} ());
App.Program = Program;
})(App || (App = {}));
// point of entry
var program = new App.Program();
program.main();
It is noticeable that there is duplication in declaring App. I want to eliminate any such repetition like this:
var App;
(function (App) {
var Greeter = (function () {
// class code
} ());
App.Greeter = Greeter;
var Program = (function () {
// class code
} ());
App.Program = Program;
})(App || (App = {}));
// point of entry
var program = new App.Program();
program.main();
When there are numerous classes, it results in unnecessary code in the output file.
Could I be making a mistake somewhere?
----Update----
The project is being built by gulp-concat and gulp-typescript, is there a package available to avoid this issue?