const enum not being converted to number
See original GitHub issueThis has been an issue since at least 3.0. I have lots of enums defined this way:
namespace SkycourtApi {
export const enum RoundRobinCompetitorStatus {
Active = 0,
Injured = 1,
DropOut = 2
}
}
then in my code I use them like this:
this.playerNotActive = model.Status !== SkycourtApi.RoundRobinCompetitorStatus.Active;
99% of the time this will get converted to the number 0 properly. But occasionally this will get left as the identifier: SkycourtApi.RoundRobinCompetitorStatus.Active
I compile my TS code twice, once for unit tests and that uses module: “amd” and once for production that uses module:“esnext” The strange thing is that I have never encountered this problem in my unit tests. I have only ever encountered this problem when creating the production code with module: “esnext”. Here is my tsconfig file that causes the problem. The only difference between the production and unit test config files is module and removeComments (true in unit test and false in production - yes I typed that correctly. The comments are removed later when webpacking).
{
"compilerOptions": {
"noImplicitAny": true,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5",
"module": "esnext",
"moduleResolution": "node",
"skipLibCheck": true,
"typeRoots": [
"../node_modules/@types",
"typings"
],
"baseUrl": "Scripts/",
"importHelpers": true,
"lib": [
"dom",
"es5",
"scripthost",
"es2015.promise"
]
},
"compileOnSave": true,
"exclude": [
"node_modules",
"obj",
"bin"
]
}
Now the really strange part, is that when this problem occurs, all I have to do is move the offending line up or down in the file and that usually fixes the problem and the production code will go back to converting the enum into the number 0.
Search Terms: const enums enum not converted to number enum not inlined Code
// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
Expected behavior: The const enums should always be converted to their numeric value.
Actual behavior: 1% of the time, they will be left as the identifier unless I move the line up or down one line in the file being compiled.
Playground Link: I will see if i can create a playground and comeback and link it here. It has been difficult to reproduce, especially since just moving the line up or down a line in the source file gets it to compile properly.
Related Issues: none
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
Possibly related. I found exported enums do not work with isolated modules and an es target. commonjs is happy for me, but only because it has no choice but to import everything. Not sure why it generates an enum object instead of a literal like it does without isolatedModules. Tested on 3.6.4 & 3.7.0-dev
test.zip
tsconfig.json
exportingFile.ts
importingFile.ts
The output:
exportingFile.js
importingModule.js
Notice that:
@Brandon-Beck Your issue is the same as https://github.com/microsoft/TypeScript/issues/16671#issuecomment-959728451, I think? It’s fixed since #40499.