-
Notifications
You must be signed in to change notification settings - Fork 12k
const enum is broken #2741
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
Comments
This is blocking me as well. version 1.0.0-beta.25.5 |
We also ran into this while upgrading to a Webpack-based/AOT-enabled build system. We have some tooling set up that automatically generates The automatically generated declare module server {
const enum Status {
OK
}
} The problem arose when we tried to use these enum values directly: export function processStatus(status: server.Status) {
switch (status) {
server.Status.OK:
// Custom code here
break;
}
} Luckily code like this was few and far-between so I solved the issue by replacing the generated enums with their constant values. This solution is of course very far from ideal: we lose type safety and the code becomes harder to read. Non-ambient Since this issue is 6 months old and is a pretty severe codegen bug some official word would be really appreciated. |
It still be very annoying bug. |
I found explanation: Const enums are preserved when using transpileModule In short: |
Now I don't use const enums(
Third way is too complex and takes much time. It is very expensive in support. Thanks! |
Dear CLI team! Please give some feedback on the issue. |
@taras-demyanets @khoden You can compare enums using
|
@sumitarora The problem isn't that we can't compare const enums, it's that the values we're comparing against aren't transpiled (and thus result in runtime errors). If we take your example (a little shortened): // Ambient declaration in another .d.ts file
namespace Validation {
export const enum A {
a, b, c
}
}
// Usage code
const x: Validation.A = Validation.A.c;
switch (x.valueOf()) {
case Validation.A.a:
console.log('a');
break;
} Should transpile to: var x = 2;
switch (x.valueOf()) {
case 0:
console.log('a');
break;
} But instead transpiles to: var x = Validation.A.c;
switch (x.valueOf()) {
case Validation.A.a:
console.log('a');
break;
} And naturally it cannot find |
I've many enums in my project and that I can't use them is a really show stopper for me to use the cli. Currently I am using pure webpack 2 and there everything is workingfine with the Enums here my TS loader is the following rule
I have a .NET MVC Project and I am using TypeLite to generate TypeScript Enums out of my C# Enums. Here a Enums.ts file is created with many Enums inside like
I am currently trying to migrate from my own webpack 2 build to the cli but currently it seems not possible. @imiuka your Solution with "preserveConstEnums" is not working here and the third solution is too complex, there I can stay with my current own WebPack Build Setup. @Gustorn why is the third Solution from @imiuka not implemented in the CLI with an addional Parameter or so and perhaps can someone explain me why my TypeScript Loader setup makes no Problems with the Enums? A working solution for such a common problem would be really nice. |
This is still a blocking issue for me.
Expected: The module not being defined at runtime is actually expected. The compiler should have replaced the enum reference with the value because this enum is defined as a constant. This is unexpected and undesirable behavior. This issue prevents me from using angular-cli in projects where I have split out enumerations into their own files for reuse. What is worse is that I can not use tools like typelite to automatically translate my enumerations from .Net to typescript.
|
The worst thing is that this is a solved problem in As a temporary workaround you can actually import const enums explicitly and it will work as expected. The problem is that most tools generate definition - and not regular - TS files. |
Indeed. As for the workaround, I agree that explicit importing should work for cases where I we modify the source file. AFAIK we would have to modify that enums file to start with This is the crux of the issue with Typelite. The enum file generated by Typelite has a structure just like in my steps. I believe I can not explicitly import a Typelite enum since it is generated and, as far as I can tell, there is no way to alter how the module is declared when it is being generated. |
We want to use CLI in out current project for better updateability, but could not switch due to the enum problem. Is there anybody investigating this problem and can say when the problem will be solved? |
@lordzero83 I found the reason: #2741 (comment) It's need for performance. Each ts file compiles separately, without information from other files. While |
The thing is, |
@khoden it would be great if the cli could provide a configuration parameter, where it ignores the performance improvements so that you can use the cli also with const enums (TypeLite) and a slower performance would be ok for me. |
This is a major issue for us, as well. The fact that it's a runtime error makes it even worse because it's really easy to miss. @hinaria , what webpack plugin are you using to do the replacement? |
This seems to still be an issue but in a different way, and it's causing issues in other module repos: swimlane/ngx-datatable#927 |
With CLI 1.5+ and Angular 5, the use of |
Tested this out and it works! Thanks for fixing it 👍 |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
OS?
Versions.
Repro steps.
then add to
main.ts
(no matter, may be any *.ts)The log given by the failure.
Actual in
main.bundle.js
console.log(Sts.A.a, Sts.A.b);
(see at Source panel of Chrome Developer Tools)Expected in
main.bundle.js
console.log(0 /* a */, 1 /* b */);
If I run
tsc
fromsrc
directory, this code will be indist/out-tsc
, as expected. TypeScript is OK!Mention any other details that might be useful.
The text was updated successfully, but these errors were encountered: