【问题标题】:How to export Typescript class in Node.js with parameter如何在 Node.js 中使用参数导出 Typescript 类
【发布时间】:2017-04-07 12:44:19
【问题描述】:

我需要的是相当于

的 Typescript
require('mytypescriptfile')(optionsObject);

但是TS码:

export class Animal {
 name: string;
 public bark(): string {
     return "bark " + this.name;
 }
 constructor(color:string) {    }
}

产生这个 JS 代码:

"use strict";
var Animal = (function () {
function Animal(color) {
}
Animal.prototype.bark = function () {
    return "bark " + this.name;
};
return Animal;
}());
exports.Animal = Animal;

生成的函数中没有参数的位置。我该怎么做?

【问题讨论】:

  • 所以你需要将 typescript 转换为 commonjs 模式......对。

标签: javascript node.js typescript


【解决方案1】:
const myTypescriptModule = require('myCOMPILEDtypescriptfile.js');

const whatIWant = new myTypescriptModule.Animal('red')

【讨论】:

    猜你喜欢
    • 2020-08-27
    • 2021-07-04
    • 2020-11-29
    • 2014-09-22
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多