推荐使用 Module

module 在现代代码中,我们建议使用模块而不是名称空间。
Namespaces and Modules

命名空间介绍

命名空间一个最明确的目的就是解决重名问题。
假设这样一种情况,当一个班上有两个名叫小明的学生时,为了明确区分它们,我们在使用名字之外,不得不使用一些额外的信息,比如他们的姓(王小明,李小明),或者他们父母的名字等等。
命名空间定义了标识符的可见范围,一个标识符可在多个名字空间中定义,它在不同名字空间中的含义是互不相干的。这样,在一个新的名字空间中可定义任何标识符,它们不会与任何已有的标识符发生冲突,因为已有的定义都处于其他名字空间中。
TypeScript 中命名空间使用 namespace 来定义,语法格式如下:

  1. namespace SomeNameSpaceName {
  2. export interface ISomeInterfaceName { }
  3. export class SomeClassName { }
  4. }

要在另外一个命名空间调用语法格式为:

  1. SomeNameSpaceName.SomeClassName;

如果一个命名空间在一个单独的 TypeScript 文件中,则应使用三斜杠 /// 引用它,语法格式如下:

  1. /// <reference path = "SomeFileName.ts" />

演示

  1. // IShape.ts
  2. namespace Drawing {
  3. export interface IShape {
  4. draw();
  5. }
  6. }
  7. // Circle.ts
  8. /// <reference path = "IShape.ts" />
  9. namespace Drawing {
  10. export class Circle implements IShape {
  11. public draw() {
  12. console.log("Circle is drawn");
  13. }
  14. }
  15. }
  16. // Triangle.ts
  17. /// <reference path = "IShape.ts" />
  18. namespace Drawing {
  19. export class Triangle implements IShape {
  20. public draw() {
  21. console.log("Triangle is drawn");
  22. }
  23. }
  24. }
  25. // TestShape.ts
  26. /// <reference path = "IShape.ts" />
  27. /// <reference path = "Circle.ts" />
  28. /// <reference path = "Triangle.ts" />
  29. function drawAllShapes(shape:Drawing.IShape) {
  30. shape.draw();
  31. }
  32. drawAllShapes(new Drawing.Circle());
  33. drawAllShapes(new Drawing.Triangle());

用 tsc 命令编译以上代码:
tsc —out app.js TestShape.ts

得到以下 JavaScript 代码:

  1. /// <reference path = "IShape.ts" />
  2. var Drawing;
  3. (function (Drawing) {
  4. var Circle = /** @class */ (function () {
  5. function Circle() {
  6. }
  7. Circle.prototype.draw = function () {
  8. console.log("Circle is drawn");
  9. };
  10. return Circle;
  11. }());
  12. Drawing.Circle = Circle;
  13. })(Drawing || (Drawing = {}));
  14. /// <reference path = "IShape.ts" />
  15. var Drawing;
  16. (function (Drawing) {
  17. var Triangle = /** @class */ (function () {
  18. function Triangle() {
  19. }
  20. Triangle.prototype.draw = function () {
  21. console.log("Triangle is drawn");
  22. };
  23. return Triangle;
  24. }());
  25. Drawing.Triangle = Triangle;
  26. })(Drawing || (Drawing = {}));
  27. /// <reference path = "IShape.ts" />
  28. /// <reference path = "Circle.ts" />
  29. /// <reference path = "Triangle.ts" />
  30. function drawAllShapes(shape) {
  31. shape.draw();
  32. }
  33. drawAllShapes(new Drawing.Circle());
  34. drawAllShapes(new Drawing.Triangle());

使用 node 命令查看输出结果为:
$ node app.js
Circle is drawn
Triangle is drawn

嵌套命名空间(总有一些不明觉历的语法)

命名空间支持嵌套,即你可以将命名空间定义在另外一个命名空间里头。

  1. namespace namespace_name1 {
  2. export namespace namespace_name2 {
  3. export class class_name { }
  4. }
  5. }

成员的访问使用点号 . 来实现,如下实例:

  1. // Invoice.ts
  2. namespace Runoob {
  3. export namespace invoiceApp {
  4. export class Invoice {
  5. public calculateDiscount(price: number) {
  6. return price * .40;
  7. }
  8. }
  9. }
  10. }
  11. // InvoiceTest.ts
  12. /// <reference path = "Invoice.ts" />
  13. var invoice = new Runoob.invoiceApp.Invoice();
  14. console.log(invoice.calculateDiscount(500));

tsc —out app.js InvoiceTest.ts

  1. var Runoob;
  2. (function (Runoob) {
  3. var invoiceApp;
  4. (function (invoiceApp) {
  5. var Invoice = /** @class */ (function () {
  6. function Invoice() {
  7. }
  8. Invoice.prototype.calculateDiscount = function (price) {
  9. return price * .40;
  10. };
  11. return Invoice;
  12. }());
  13. invoiceApp.Invoice = Invoice;
  14. })(invoiceApp = Runoob.invoiceApp || (Runoob.invoiceApp = {}));
  15. })(Runoob || (Runoob = {}));
  16. /// <reference path = "Invoice.ts" />
  17. var invoice = new Runoob.invoiceApp.Invoice();
  18. console.log(invoice.calculateDiscount(500));

使用 node 命令查看输出结果为:
$ node app.js
200