Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is TypeScript not using revealing module pattern for classes?

Tags:

typescript

Is TypeScript not using revealing module pattern for classes? I expected different result from this code.

class Test {

    private privateProperty: any;

    public publicProperty: any;     
}

generates this:

var Test = (function () {
    function Test() { }
    return Test;
})();

I expected something like this:

var test = (function(){
    var privateProperty;
    var publicProperty;

    return {
        publicProperty: publicProperty;
    };

})();
like image 426
epitka Avatar asked Jan 14 '23 18:01

epitka


1 Answers

RMP is not appropriate for class-based design. module does what you want:

module myMod {
    var x = 31;
    export var y = x + 15;
}

Generates:

var myMod;
(function (myMod) {
    var x = 31;
    myMod.y = x + 15;
})(myMod || (myMod = {}));

Salient features here:

  • Privates are captured with a closure
  • Public members are visible as own properties of the object
  • The object is created in an IIFE
like image 79
Ryan Cavanaugh Avatar answered Jan 23 '23 02:01

Ryan Cavanaugh