如何在多个文件的输出typescript中优化模块命名空间,使用gulp(How optimize module namespace in output typescript of multiple

编程入门 行业动态 更新时间:2024-10-10 14:30:21
如何在多个文件的输出typescript中优化模块命名空间,使用gulp(How optimize module namespace in output typescript of multiple files, use gulp)

我遇到了一个问题,如果使用方法: 一个文件 -在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-concatgulp-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

更多推荐

本文发布于:2023-08-02 08:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372742.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   模块   文件   如何在   空间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!