【问题标题】:Is it possible to combine multiple internal modules to one external?是否可以将多个内部模块组合成一个外部模块?
【发布时间】:2015-09-10 11:05:20
【问题描述】:

我想要实现的是有这样的布局:

// Shape.ts
module Shape {
    export interface Shape {
        area(): number;        
    }
}

// Rectangle.ts
module Shape {
    export class Rectangle implements Shape {
        constructor(private x:number, private y: number) {}
        area(): number {
            return this.x * this.y;
        }
    }
}

// Square.ts
module Shape {
    export class Square implements Shape  {
        constructor(private x:number) {}
        area(): number {
            return Math.pow(this.x, 2);
        }
    }
}

// geometry.ts
/// <reference path="Shape.ts" />
/// <reference path="Rectangle.ts" />
/// <reference path="Square.ts" />
export = Shape.Rectangle;
export = Shape.Square;

我在常规内部模块中的代码最多。而且,为了构建一个 Node.js 模块,我有一个外部模块,它导出所有的东西,旨在供 Node 模块用户使用。

但目前,当我调用 tsc -m commonjs --outFile geometrylib.js geometry.ts 时,我得到一个 geometrylib.js 没有任何 module.exports 语句,因为它应该在 CommonJS 构建中。

当前版本的 TypeScript 是否有可能?

ps
tsc --version 给我message TS6029: Version 1.6.0-beta。

【问题讨论】:

    标签: javascript node.js typescript


    【解决方案1】:

    是否可以将多个内部模块组合成一个外部模块?

    而不是

    export = Shape.Rectangle;
    export = Shape.Square;
    

    你可以这样做:

    export = Shape;
    

    但是我建议你不要混合内部和外部模块......就像你的消费者尊重npm并接受commonjs一样。

    @)--

    【讨论】:

      猜你喜欢
      • 2014-05-11
      • 2017-10-17
      • 2014-04-05
      • 2014-02-11
      • 2019-10-19
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多