【问题标题】:How to include typescript class in node.js app?如何在 node.js 应用程序中包含 typescript 类?
【发布时间】:2015-03-10 17:45:14
【问题描述】:

在 node.js 文件中包含 typescript 类定义的最佳方式是什么?

假设我有这个代码:

class Car{
    public Color;

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

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

var MyCar = new Car("green");

我知道 require() 会返回一个对象,但我不需要对象我只需要知道 Car 的定义。

最好的方法是什么?

【问题讨论】:

  • 让你的类可见的最简单方法是define a module,但我不确定你是如何公用结果的。 module.exports = MyModule我猜?

标签: javascript node.js typescript


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 2017-10-05
    • 2016-04-12
    相关资源
    最近更新 更多