I have this TS code:
export namespace Constants
{
export var x = 0;
}
If I compile it with 'tsc' I get this JS code:
"use strict";
exports.__esModule = true;
exports.Constants = void 0;
var Constants;
(function (Constants) {
Constants.x = 0;
})(Constants = exports.Constants || (exports.Constants = {}));
My question is. Why 'tsc' compiles a namespace into an IIFE instead of a simple and much more readable object like this?
var Constants = {};
Constants.x = 0;
Is there any particular reason?
There are other questions similar to this. Except that they don't give any true answer. What makes this question different is that a namespace is meant to be only a simple container to segregate code, then it must be as simple and readable as possible and an IIFE isn't the best solution to do that.