Closed
Description
Consider the following code
export class Test {
private x: number;
constructor() {
this.x = 42;
}
f() : number {
return this.x;
}
}
Typescript generates the following JS
var Test = (function () {
function Test() {
this.x = 42;
}
Test.prototype.f = function () {
return this.x;
};
return Test;
})();
exports.Test = Test;
Obviously, one could run something like new Test().x
in a JS library, and private
invariant would fail. I think it's not a kind of behaviour one would expect from private
, at least in export
ed classes. The fix is quite simple:
var Test = (function () {
var _private = {};
function Test() {
_private.x = 42;
}
Test.prototype.f = function () {
return _private.x;
};
return Test;
})();
exports.Test = Test;