Skip to content

enums are not compiled as numbers when declaration flag is false in tsconfig.json #11202

Closed
@akosbordas

Description

@akosbordas

TypeScript Version: 2.02

Expected behavior:

I have a const enum in typescript:

const enum LogLevel {
    TRACE = 0,
    DEBUG = 1,
    INFO = 2,
    WARN = 3,
    ERROR = 4,
    SILENT = 5
}

Based on the typescript spec the following field:

private foo: number = LogLevel.DEBUG;

should be compiled as:

this.foo = 1 /* DEBUG */;

Actual behavior:

When I use tsc from the command line (Windows) it works as expected. But when it is compiled with awesome-typescript-loader in a webpack project (which uses the typescript.js from node_modules as opposed to the tsc.js which is used by tsc), then the enum constant is not getting inlined:

this.foo = LogLevel.DEBUG;

It turned out it was caused by the declaration option in tsconfig.json. If it is set to false, the two compilations produce the above inconsistent result. But when it is set to true, it works as expected. Not sure why this flag has such an effect on the outcome.

I created a github project where the problem is reproducible.

https://github.com/akosbordas/typescript-enum-issue

After you changed the declaration flag you just have to run ng build and check the main.bundle.js in dist folder.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ExternalRelates to another program, environment, or user action which we cannot control.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions