typescript 打字稿中的 module.exports

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12696236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:47:39  来源:igfitidea点击:

module.exports in typescript

typescriptimportcommonjs

提问by Kersten

does somebody know how to do a module.exports?

有人知道如何做 module.exports 吗?

I tried some different ways ending up with

我尝试了一些不同的方法,结果是

export class Greeter {}

which will compile to

这将编译为

exports.Greeter = Greeter;

But what I really want is this:

但我真正想要的是:

exports = Greeter;

So that I can use it like this:

这样我就可以这样使用它:

import { Greeter } from "greeter";

const greeter = new Greeter();

and not

并不是

import { Greeter } from "greeter";

const greeter = new Greeter.Greeter();

Is this possible with Typescript?

这可以用打字稿吗?

采纳答案by Benny Neugebauer

You can export a single class in TypeScript like this:

您可以像这样在 TypeScript 中导出单个类:

class Person {

  private firstName: string;
  private lastName: string;

  constructor(firstName: string, lastName: string) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  public getFullName() {
    return `${this.firstName} ${this.lastName}`;
  }
}

export = Person;

And here is how it's going to be used:

这是它将如何使用:

var Person = require('./dist/commonjs/Person.js');

var homer = new Person('Homer', 'Simpson');
var name = homer.getFullName();

console.log(name); // Homer Simpson

To be complete, here is my tsconfig.json(I am using TypeScript v2.0.3):

为了完整起见,这是我的tsconfig.json(我使用的是 TypeScript v2.0.3):

{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "dist/commonjs",
    "rootDir": "src/ts",
    "target": "es5"
  },
  "exclude": [
    "dist",
    "node_modules"
  ]
}

回答by Kersten

This has now been implemented and is ready in TypeScript 0.9 :)

这现已在 TypeScript 0.9 中实现并准备就绪 :)

回答by Daniel Flower

So I think I've found a workaround. Just wrap the keyword 'module' in parentheses in your .ts file:

所以我想我已经找到了解决方法。只需将关键字 'module' 括在 .ts 文件中的括号中:

declare var module: any;
(module).exports = MyClass;

The generated javascript file will be exactly the same:

生成的 javascript 文件将完全相同:

(module).exports = MyClass;

Note, better than declaring var module yourself, download the node.d.ts definition fileand stick it in the same directory as your typescript file. Here is a complete sample of an express node.js routing file which assumes node.d.ts is in same directory:

注意,比自己声明 var 模块更好,下载node.d.ts 定义文件并将其粘贴在与打字稿文件相同的目录中。这是一个快速 node.js 路由文件的完整示例,它假定 node.d.ts 位于同一目录中:

/// <reference path="node.d.ts" />
var SheetController = function () {
    this.view = function (req, res) {
        res.render('view-sheet');
    };
};
(module).exports = SheetController;

I can then new up a SheetController and (using express) assign the view method:

然后我可以新建一个 SheetController 并(使用 express)分配视图方法:

var sheetController = new SheetController();
app.get('/sheet/view', sheetController.view);

I suppose any keyword can be escaped using this pattern:

我想任何关键字都可以使用这种模式进行转义:

declare var reservedkeyword: any;
(reservedkeyword).anything = something;

回答by Peter Burns

It's ugly and hacky, but you can still do:

它既丑陋又笨拙,但您仍然可以这样做:

class Greeter {}
declare var exports:any;
exports = Greeter;

Which compiles into:

编译成:

var Greeter = (function () {
    function Greeter() { }
    return Greeter;
})();
exports = Greeter;