TypeScript tsconfig.json preserveConstEnums


Example

Typescript supports costant enumerables, declared through const enum.

This is usually just syntax sugar as the costant enums are inlined in compiled JavaScript.

For instance the following code

const enum Tristate {
    True,
    False,
    Unknown
}

var something = Tristate.True;

compiles to

var something = 0;

Although the perfomance benefit from inlining, you may prefer to keep enums even if costant (ie: you may wish readability on development code), to do this you have to set in tsconfig.json the preserveConstEnums clausole into the compilerOptions to true.

{
    "compilerOptions": {
        "preserveConstEnums" = true,
        ...
    },
    "exclude": [
        ...
    ]
}

By this way the previous example would be compiled as any other enums, as shown in following snippet.

var Tristate;
(function (Tristate) {
    Tristate[Tristate["True"] = 0] = "True";
    Tristate[Tristate["False"] = 1] = "False";
    Tristate[Tristate["Unknown"] = 2] = "Unknown";
})(Tristate || (Tristate = {}));

var something = Tristate.True