如何在 node.js 应用程序中包含打字稿 class?

How to include typescript class in node.js app?

在 node.js 文件中包含打字稿 class 定义的最佳方法是什么?

假设我有这个代码:

class Car{
    public Color;

    constructor(aColor:string){
        this.Color = aColor;
    }
}

我希望能够以这种方式创建 Car 的实例:

var MyCar = new Car("green");

我知道 require() 会 return 一个对象,但我不需要对象,我只需要知道汽车的定义。

最好的方法是什么?

car.ts 中:

class Car{
    public Color;

    constructor(aColor:string){
        this.Color = aColor;
    }
}
export = Car;

在其他文件中:

import Car = require('./car');
var MyCar = new Car("green");

--module commonjs 编译两个文件。

有关外部模块的更多信息:https://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1