设计一个模块的目的是组织用TypeScript编写的代码。模块大致分为-
- 内部模块
- 外部模块
内部模块
内部模块来自早期版本的Typescript。这用于将类,接口,函数逻辑分组为一个单元,并可以导出到另一个模块中。在最新版本的TypeScript中,此逻辑分组被命名为命名空间。因此内部模块已经过时了,我们可以使用命名空间。内部模块仍受支持,但建议在内部模块上使用名称空间。
内部模块语法(旧)
module TutorialPoint {
export function add(x, y) {
console.log(x+y);
}
}
命名空间语法(新)
namespace TutorialPoint {
export function add(x, y) { console.log(x + y);}
}
两种情况下生成的JavaScript是相同的
var TutorialPoint;
(function (TutorialPoint) {
function add(x, y) {
console.log(x + y);
}
TutorialPoint.add = add;
})(TutorialPoint || (TutorialPoint = {}));
外部模块
TypeScript中存在外部模块,用于指定和加载多个外部js文件之间的依赖关系。如果仅使用一个js文件,则外部模块无关。传统上,JavaScript文件之间的依赖性管理是使用浏览器脚本标签(<script> </ script>)完成的。但这是不可扩展的,因为它在加载模块时非常线性。这意味着没有异步加载文件的方式,而没有异步选项来加载模块。当您为服务器(例如NodeJ)编程js时,您甚至没有脚本标签。
从单个主JavaScript文件加载从属js文件有两种方案。
- 客户端-RequireJs
- 服务器端-NodeJ
选择模块加载器
为了支持加载外部JavaScript文件,我们需要一个模块加载器。这将是另一个js库。对于浏览器,最常用的库是RequieJS。这是AMD(异步模块定义)规范的实现。AMD可以将所有文件分别加载,即使它们相互依赖,也不必单独加载文件。
定义外部模块
在以CommonJS或AMD为目标的TypeScript中定义外部模块时,每个文件都被视为一个模块。因此,将内部模块与外部模块一起使用是可选的。
如果要将TypeScript从AMD迁移到CommonJs模块系统,则不需要其他工作。您唯一需要更改的就是编译器标志,与JavaScript不同,从CommonJs迁移到AMD或反之亦然。
声明外部模块的语法使用关键字“ export”和“ import”。
句法
//FileName : SomeInterface.ts
export interface SomeInterface {
//code declarations
}
要在另一个文件中使用声明的模块,请使用import关键字,如下所示。仅指定文件名,不使用扩展名。
import someInterfaceRef = require(“./SomeInterface”);
例
让我们通过一个例子来理解这一点。
// IShape.ts
export interface IShape {
draw();
}
// Circle.ts
import shape = require("./IShape");
export class Circle implements shape.IShape {
public draw() {
console.log("Cirlce is drawn (external module)");
}
}
// Triangle.ts
import shape = require("./IShape");
export class Triangle implements shape.IShape {
public draw() {
console.log("Triangle is drawn (external module)");
}
}
// TestShape.ts
import shape = require("./IShape");
import circle = require("./Circle");
import triangle = require("./Triangle");
function drawAllShapes(shapeToDraw: shape.IShape) {
shapeToDraw.draw();
}
drawAllShapes(new circle.Circle());
drawAllShapes(new triangle.Triangle());
编译用于AMD系统的主TypeScript文件的命令是-
tsc --module amd TestShape.ts
编译时,它将为AMD生成以下JavaScript代码。
文件:IShape.js
//Generated by typescript 1.8.10
define(["require", "exports"], function (require, exports) {
});
文件:Circle.js
//Generated by typescript 1.8.10
define(["require", "exports"], function (require, exports) {
var Circle = (function () {
function Circle() {
}
Circle.prototype.draw = function () {
console.log("Cirlce is drawn (external module)");
};
return Circle;
})();
exports.Circle = Circle;
});
文件:Triangle.js
//Generated by typescript 1.8.10
define(["require", "exports"], function (require, exports) {
var Triangle = (function () {
function Triangle() {
}
Triangle.prototype.draw = function () {
console.log("Triangle is drawn (external module)");
};
return Triangle;
})();
exports.Triangle = Triangle;
});
文件:TestShape.js
//Generated by typescript 1.8.10
define(["require", "exports", "./Circle", "./Triangle"],
function (require, exports, circle, triangle) {
function drawAllShapes(shapeToDraw) {
shapeToDraw.draw();
}
drawAllShapes(new circle.Circle());
drawAllShapes(new triangle.Triangle());
});
编译Commonjs系统的主要TypeScript文件的命令是
tsc --module commonjs TestShape.ts
编译时,它将为Commonjs生成以下JavaScript代码。
文件:Circle.js
//Generated by typescript 1.8.10
var Circle = (function () {
function Circle() {
}
Circle.prototype.draw = function () {
console.log("Cirlce is drawn");
};
return Circle;
})();
exports.Circle = Circle;
文件:Triangle.js
//Generated by typescript 1.8.10
var Triangle = (function () {
function Triangle() {
}
Triangle.prototype.draw = function () {
console.log("Triangle is drawn (external module)");
};
return Triangle;
})();
exports.Triangle = Triangle;
文件:TestShape.js
//Generated by typescript 1.8.10
var circle = require("./Circle");
var triangle = require("./Triangle");
function drawAllShapes(shapeToDraw) {
shapeToDraw.draw();
}
drawAllShapes(new circle.Circle());
drawAllShapes(new triangle.Triangle());
输出量
Cirlce is drawn (external module) Triangle is drawn (external module)