1. // 文件名 : SomeInterface.ts
  2. export interface SomeInterface {
  3. // 代码部分
  4. }
  5. 另一个文件中
  6. import someInterfaceRef = require("./SomeInterface");

实例

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

使用 tsc 命令编译以上代码(AMD):
tsc —module amd TestShape.ts

  1. IShape.js 文件代码:
  2. define(["require", "exports"], function (require, exports) {
  3. });
  4. Circle.js 文件代码:
  5. define(["require", "exports"], function (require, exports) {
  6. var Circle = (function () {
  7. function Circle() {
  8. }
  9. Circle.prototype.draw = function () {
  10. console.log("Cirlce is drawn (external module)");
  11. };
  12. return Circle;
  13. })();
  14. exports.Circle = Circle;
  15. });
  16. Triangle.js 文件代码:
  17. define(["require", "exports"], function (require, exports) {
  18. var Triangle = (function () {
  19. function Triangle() {
  20. }
  21. Triangle.prototype.draw = function () {
  22. console.log("Triangle is drawn (external module)");
  23. };
  24. return Triangle;
  25. })();
  26. exports.Triangle = Triangle;
  27. });
  28. TestShape.js 文件代码:
  29. define(["require", "exports", "./Circle", "./Triangle"],
  30. function (require, exports, circle, triangle) {
  31. function drawAllShapes(shapeToDraw) {
  32. shapeToDraw.draw();
  33. }
  34. drawAllShapes(new circle.Circle());
  35. drawAllShapes(new triangle.Triangle());
  36. });

使用 tsc 命令编译以上代码(Commonjs):
tsc —module commonjs TestShape.ts

  1. 得到以下 JavaScript 代码:
  2. Circle.js 文件代码:
  3. var Circle = (function () {
  4. function Circle() {
  5. }
  6. Circle.prototype.draw = function () {
  7. console.log("Cirlce is drawn");
  8. };
  9. return Circle;
  10. })();
  11. exports.Circle = Circle;
  12. Triangle.js 文件代码:
  13. var Triangle = (function () {
  14. function Triangle() {
  15. }
  16. Triangle.prototype.draw = function () {
  17. console.log("Triangle is drawn (external module)");
  18. };
  19. return Triangle;
  20. })();
  21. exports.Triangle = Triangle;
  22. TestShape.js 文件代码:
  23. var circle = require("./Circle");
  24. var triangle = require("./Triangle");
  25. function drawAllShapes(shapeToDraw) {
  26. shapeToDraw.draw();
  27. }
  28. drawAllShapes(new circle.Circle());
  29. drawAllShapes(new triangle.Triangle());
  30. 输出结果为:
  31. Cirlce is drawn (external module)
  32. Triangle is drawn (external module)