在 TypeScript 中将类导出为 Node.js 模块 [英] Export class as Node.js module in TypeScript

查看:26
本文介绍了在 TypeScript 中将类导出为 Node.js 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉 TypeScript 中的 export 关键字,以及两种使用 TypeScript 从 Node 模块导出事物的规范方法(当然,也可以使用 TypeScript 模块,但它们甚至离我正在寻找的更远):

I'm familiar with the export keyword in TypeScript, and two canonical ways of exporting things from Node modules using TypeScript (of course, the TypeScript modules can be used as well, but they are even further from what I'm looking for):

export class ClassName { }

和一系列

export function functionName () { }

然而,我通常编写模块的方式是:

However, the way I usually write my modules, so that they are later imported as instantiable closures, is:

var ClassName = function () { };
ClassName.prototype.functionName = function () { };
module.exports = ClassName;

有没有办法使用 TypeScript 导出语法来做到这一点?

Is there a way I can do this using the TypeScript export syntax?

推荐答案

您可以在 TypeScript 0.9.0 中非常简单地做到这一点:

You can do that quite simply in TypeScript 0.9.0 :

class ClassName { 
    functionName () { }
}

export = ClassName;

这篇关于在 TypeScript 中将类导出为 Node.js 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆