Change "export enum" to "export const enum" in Enums.ts

Issue #90 resolved
Bryce Abbott
created an issue

Hi Excellent package, love it. I can change my database structure, and with a few clicks, reflect the change in my Typescript. However, updating to Typescript 1.5 broke my enum code, see: https://github.com/Microsoft/TypeScript/issues/2941 I suggest that when Typelite produces Enums.ts instead of writing

export enum ...

it writes

export const enum ...

An example

namespace SolsofSpa
{
    public class Helper
    {
        [TypeLite.TsEnum]
        public enum enumTransactionType : byte { Cheque = 0, Deposit = 1, BankStatementTransaction = 3, GeneralJournal = 4, Invoice = 5, PayInvoice = 6 };

in enums.ts becomes

module SolsofSpa.Helper {
    export enum enumTransactionType {
        Cheque = 0,
        Deposit = 1,
        BankStatementTransaction = 3,
        GeneralJournal = 4,
        Invoice = 5,
        PayInvoice = 6
    }

Then this code code in TypeScript:

var transactionType: SolsofSpa.Helper.enumTransactionType = SolsofSpa.Helper.enumTransactionType.Cheque;

is compiled to this javascript:

var transactionType = SolsofSpa.Helper.enumTransactionType.Cheque;

which errors at runtime ReferenceError: SolsofSpa is not defined. If I edit enums.ts to change "export enum" to "export const enum"

    export const enum enumTransactionType {
        Cheque = 0,
        Deposit = 1,
        BankStatementTransaction = 3,
        GeneralJournal = 4,
        Invoice = 5,
        PayInvoice = 6
    }

then the compiled javascript becomes:

var transactionType = 0 /* Cheque */;

and there is no runtime error

Comments (10)

  1. Edward Brey

    What is the best approach when you want the non-const enums so that you can reflect on them in the JavaScript code? Is there an override to cause the "const" to not be output?

  2. Jesse Thompson

    Figured it out from another thread. For anyone who is curious on how to turn this behavior off, you can to add .AsConstEnums(false) to your .tt file alongside .WithReference("Enums.ts")

  3. Log in to comment