module.exports in typescript

2019-01-21 18:32发布

问题:

does somebody know how to do a 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 = module("Greeter");
var greeter = new Greeter();

and not

import Greeter = module("Greeter");
var greeter = new Greeter.Greeter();

Is this possible with Typescript?

回答1:

You can export a single class in TypeScript like this:

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):

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


回答2:

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



回答3:

Currently no. This is a limitation in TypeScript and a limitation in the current straw-man module specification being considered by ECMA which TypeScript is patterned after.

This would be a great suggestion to make on CodePlex.



回答4:

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

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

The generated javascript file will be exactly the same:

(module).exports = MyClass;

Note, better than declaring var module yourself, download the node.d.ts definition file and 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:

/// <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:

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;


回答5:

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;


回答6:

try this boilerplate app template https://github.com/ntheile/TypeScript-Backbone-Require-Boilerplate