Search code examples
typescriptenumstsc

Typescript not compiling enums to numbers


I'm trying to use typescript to essentially write json data with a schema, so I need the Typescript compiler to compile enums into numbers in the js outputs but I can't get this to work.

I've tried a good few things already, this post is similar to the issue I'm having but I'm not using awesome-typescript-loader, I've also tried everything this stack overflow post but no joy. My ts-config is below

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitAny": false,
    "removeComments": true,
    "preserveConstEnums": false,
    "sourceMap": false
},
    "include": [
      "src/**/*"
    ]
  }

I'm simply calling tsc on the command line in the root directory, it compiles everything but enums are compiled as MyEnum_1.MyEnum.FirstValue. Other things I've tried;

  • Changing the module to various different options
  • Changing the target to various different options
  • Adding different options and playing around with different lib values.

I'm running tsc -v 3.6.4 and have run out of ideas, has anyone had a similar issue?

EDIT: Below is an example of the type of enum I'm importing

export enum MyEnum {
    Undefined,
    FirstValue,
    SecondValue,
    ThirdValue
}

EDIT: My an example of a typescript file would be...

import { MyEnum } from "../../src/models/MyEnum";
import { ItemType } from "../../src/models/ItemType";

var item: ItemType = { 
    "id": "00000000-0000-0000-0000-000000000000",
    "value": MyEnum.FirstValue
}

I would expect this to compile to...

"use strict";
exports.__esModule = true;
var MyEnum_1 = require("../../src/models/MyEnum");
var ItemType_1 = require("../../src/models/ItemType");
var item = {
    "id": "00000000-0000-0000-0000-000000000000",
    "value": 1
};

Instead it compiles to;

"use strict";
exports.__esModule = true;
var MyEnum_1 = require("../../src/models/MyEnum");
var ItemType_1 = require("../../src/models/ItemType");
var item = {
    "id": "00000000-0000-0000-0000-000000000000",
    "value": MyEnum_1.MyEnum.FirstValue
};

I pretty much don't care how it compiles as long as the enum compiles to a number.


Solution

  • You're looking for const enums:

    export const enum MyEnum {
        Undefined,
        FirstValue,
        SecondValue,
        ThirdValue
    }
    var item = { 
        "id": "00000000-0000-0000-0000-000000000000",
        "value": MyEnum.FirstValue
    }
    

    compiles to

    var MyEnum;
    (function (MyEnum) {
    // stuff
    })(MyEnum || (MyEnum = {}));
    
    var item = {
        "id": "00000000-0000-0000-0000-000000000000",
        "value": 1 /* FirstValue */
    };
    

    and if you want to skip the enum declaration altogether, make it ambient as well:

    export declare const enum MyEnum {
       ...
    }
    var item = { 
        "id": "00000000-0000-0000-0000-000000000000",
        "value": MyEnum.FirstValue
    }
    

    gives just

    var item = {
        "id": "00000000-0000-0000-0000-000000000000",
        "value": 1 /* FirstValue */
    };