Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
akosbordas opened this issue Sep 28, 2016 · 2 comments
Labels
External Relates to another program, environment, or user action which we cannot control.

Comments

@akosbordas
Copy link

akosbordas commented Sep 28, 2016

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.

@akosbordas akosbordas changed the title enums are not compiled as number when declaration flag is false in tsconfig.json enums are not compiled as numbers when declaration flag is false in tsconfig.json Sep 28, 2016
@RyanCavanaugh RyanCavanaugh added the External Relates to another program, environment, or user action which we cannot control. label Sep 28, 2016
@RyanCavanaugh
Copy link
Member

This is an issue with how awesome-typescript-loader is using the compiler API.

awesome-typescript-loader uses the TypeScript transpileModule function in "fast mode"; "fast mode" is enabled when the declaration flag is not set. transpileModule does a pure syntactic transform of each file; in this mode, const enums are not resolved (because its a per-file transform) and their values are not inlined.

transpileModule should not be used unless an error-free compilation of the same sources has occurred under the --isolatedModules flag, which verifies that it is safe to use transpileModule on a particular compilation.

I would log a bug with awesome-typescript-loader -- their behavior here is not sound. Hopefully there's some way to configure it to not use transpileModule?

@akosbordas
Copy link
Author

Thank you for your answer! I created an issue for awesome-typescript-loader as well. #245

@mhegazy mhegazy closed this as completed Sep 29, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
External Relates to another program, environment, or user action which we cannot control.
Projects
None yet
Development

No branches or pull requests

3 participants