module.exports in typescript

后端 未结 4 1908
半阙折子戏
半阙折子戏 2020-12-13 03:30

does somebody know how to do a module.exports?

I tried some different ways ending up with

export class Greeter         


        
4条回答
  •  不思量自难忘°
    2020-12-13 03:42

    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:

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

提交回复
热议问题