Search code examples
moduletypescriptamd

What is the equivalent typescript for my module?


I am very close to getting what I want but it's not quite there. I have this:

export class RichTextArea {
    text: string;

    constructor(params: any)
    {
        this.text = params.text;
    }
}

which is generating this (AMD):

define(["require", "exports"], function (require, exports) {
    "use strict";
    var RichTextArea = (function () {
        function RichTextArea(params) {
            self.text= params.text;
        }
        return RichTextArea;
    }());
    exports.RichTextArea = RichTextArea;
});

I need it to generate something that looks like this (see change to export):

define(["require", "exports"], function (require, exports) {
    "use strict";
    var RichTextArea = (function () {
        function RichTextArea(params) {
            self.text = params.text;
        }
        return RichTextArea;
    }());
    return RichTextArea; //I need this so that it is immediately available
});

What do I have to change in my TS to achieve this? When I import my module, I don't want to have to say mymodule.RichTextArea(params), I want to be able to say mymodule(params)


Solution

  • Use this:

    class RichTextArea {
        text: string;
    
        constructor(params: any)
        {
            this.text = params.text;
        }
    }
    
    export = RichTextArea;
    

    Outputs this code:

    define(["require", "exports"], function (require, exports) {
        "use strict";
        var RichTextArea = (function () {
            function RichTextArea(params) {
                this.text = params.text;
            }
            return RichTextArea;
        }());
        return RichTextArea;
    });