- amd
- commonjs
- ……
// 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());
tsc --module amd TestShape.ts
// IShape.js
define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
});
// Circle.js
define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Circle = void 0;
var Circle = /** @class */ (function () {
function Circle() {
}
Circle.prototype.draw = function () {
console.log("Cirlce is drawn (external module)");
};
return Circle;
}());
exports.Circle = Circle;
});
// Triangle.js
define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Triangle = void 0;
var Triangle = /** @class */ (function () {
function Triangle() {
}
Triangle.prototype.draw = function () {
console.log("Triangle is drawn (external module)");
};
return Triangle;
}());
exports.Triangle = Triangle;
});
// TestShape.js
define(["require", "exports", "./Circle", "./Triangle"], function (require, exports, circle, triangle) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function drawAllShapes(shapeToDraw) {
shapeToDraw.draw();
}
drawAllShapes(new circle.Circle());
drawAllShapes(new triangle.Triangle());
});
tsc --module commonjs TestShape.ts
// IShape.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// Circle.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Circle = void 0;
var Circle = /** @class */ (function () {
function Circle() {
}
Circle.prototype.draw = function () {
console.log("Cirlce is drawn (external module)");
};
return Circle;
}());
exports.Circle = Circle;
// Triangle.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Triangle = void 0;
var Triangle = /** @class */ (function () {
function Triangle() {
}
Triangle.prototype.draw = function () {
console.log("Triangle is drawn (external module)");
};
return Triangle;
}());
exports.Triangle = Triangle;
// TestShape.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var circle = require("./Circle");
var triangle = require("./Triangle");
function drawAllShapes(shapeToDraw) {
shapeToDraw.draw();
}
drawAllShapes(new circle.Circle());
drawAllShapes(new triangle.Triangle());
- 声明文件以
.d.ts
为后缀 keyof
是 ts 中的一个操作符,用于获取一个类型的所有键作为字符串字面量类型的集合
interface Person {
name: string;
age: number;
}
type PersonKeys = keyof Person; // type PersonKeys = "name" | "age"
function getPropertyValue(person: Person, key: PersonKeys): any {
return person[key];
}
const bob: Person = {
name: "Bob",
age: 25
};
console.log(getPropertyValue(bob, "name")); // 输出: Bob
console.log(getPropertyValue(bob, "age")); // 输出: 25
// 下面的代码将导致编译错误,因为 "address" 不是 Person 的一个键
// console.log(getPropertyValue(bob, "address"));
// 报错:类型“"address"”的参数不能赋给类型“keyof Person”的参数。ts(2345)