我遇到了一个问题,如果使用方法: 一个文件 -在typescript项目中有一个类 ,如下例所示。
File Greeter.ts
module App { export class Greeter { constructor(public greeting: string) { } public greet() { return "<h1>" + this.greeting + "</h1>"; } }; }文件Program.ts
module App { export class Program { private _greeter = new Greeter("Hello, world!"); public main() { document.body.innerHTML = this._greeter.greet(); } }; }结果文件
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();可以看出重复申报App 。 我想排除这样的重复:
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();否则,当大量的类时,输出文件中会出现很多过多的代码。
也许我错了?
---- ----更新
该项目是通过gulp-concat和gulp-typescript构建的,也许存在包允许避免这种情况?
github上的示例
I encountered a problem, if use methodology: one file - one class in typescript project, as 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();As can be seen duplication declaration App. I would like exclude any 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();Otherwise, when a large amount of classes, it turns out a lot of excess code in the output file.
Maybe I'm wrong somewhere?
----Update----
The project is building by gulp-concat and gulp-typescript, maybe exist package allowing avoid this?
example on github
最满意答案
否则,当大量的类时,输出文件中会出现很多过多的代码。 也许我错了?
不,这是设计的。 基本上,打字稿允许每个部分单独运行。
警告
请尝试迁移到模块。 更多https://basarat.gitbooks.io/typescript/content/docs/tips/outFile.html
Otherwise, when a large amount of classes, it turns out a lot of excess code in the output file. Maybe I'm wrong somewhere?
No. This is by design. Basically typescript allows each section to run in isolation.
Warning
Please try and migration to modules . More https://basarat.gitbooks.io/typescript/content/docs/tips/outFile.html
更多推荐
发布评论