const enum is not work
project like:
├─dist
│ └─main.js
├─src
│ └─main.ts
└─tsconfig.json
tsconfig.json is
{
"compilerOptions": {
"module": "amd",
"lib": [
"dom",
"esnext"
],
"target": "esnext",
"removeComments": true,
"outDir": "./dist"
},
"include": [
"./src/*.ts"
]
}
main.ts is
const enum TE {
A = 1,
B = 2,
C = 3,
}
console.log(TE.A)
console.log(TE.B)
tsc out file main.js is:
console.log(1);
console.log(2);
tsgo out file main.js is:
var TE;
(function (TE) {
TE[TE["A"] = 1] = "A";
TE[TE["B"] = 2] = "B";
TE[TE["C"] = 3] = "C";
})(TE || (TE = {}));
console.log(TE.A);
console.log(TE.B);
Problem:
TEshould not appear in the resultTE.AandTE.Bare not replaced with1and2
I believe you are using tsc for compilation, so you should include these additional compile options as below:
"preserveConstEnums": false
"isolatedModules": false
If you are using Babel/SWC/Next.j then avoid using const enum and include below :
"preserveConstEnums": true
"isolatedModules": true
I believe you are using tsc for compilation, so you should include these additional compile options as below:
"preserveConstEnums": false "isolatedModules": falseIf you are using Babel/SWC/Next.j then avoid using const enum and include below :
"preserveConstEnums": true "isolatedModules": true
Tks. the problem remains. Perhaps there's another factor.
What is the current issue
What is the current issue
I changed tsconfig.json to
{
"compilerOptions": {
"module": "amd",
"lib": [
"dom",
"esnext"
],
"target": "esnext",
"removeComments": true,
"outDir": "./dist",
"preserveConstEnums": false,
"isolatedModules": false
},
"include": [
"./src/*.ts"
]
}
tsgo output file main.js is:
var TE;
(function (TE) {
TE[TE["A"] = 1] = "A";
TE[TE["B"] = 2] = "B";
TE[TE["C"] = 3] = "C";
})(TE || (TE = {}));
console.log(TE.A);
console.log(TE.B);
Problem:
TEshould not appear in the resultTE.AandTE.Bare not replaced with1and2
The output file is expected to have only 2 lines:
console.log(1);
console.log(2);
My understanding is that we aren't going to be doing const enum inlining anymore, as it doesn't mesh well with concurrent checking and emitting.
@jakebailey so what is the intended way to use existing libraries that have const enums? For example @homebridge/ciao has this definition:
https://github.com/homebridge/ciao/blob/11e5abcd8656ce627517275013cc44bbb1bf7769/src/index.ts#L37-L40
export const enum Protocol {
TCP = "tcp",
UDP = "udp",
}
If I try to use the string directly, tsgo complains about a mismatch of types:
src/lib/server.ts:543:9 - error TS2322: Type '"tcp"' is not assignable to type 'Protocol | undefined'.
543 protocol: "tcp",
~~~~~~~~
If I use the imported const enum with isolatedModules, tsgo complains about the const enum:
src/lib/server.ts:543:19 - error TS2748: Cannot access ambient const enums when 'isolatedModules' is enabled.
543 protocol: Protocol.TCP,
~~~~~~~~
Found 1 error in src/lib/server.ts:543
And if I use the imported const enum without isolatedModules, I end up with an undefined reference in the compiled JS output:
Er, well that has always been a really terrible idea. You're inlining someone else's enums into your code, and then if they change, your published code will be wrong.
But, the fact that this is errors out like this is sort of a problem, yeah. Worth a separate issue.
I guess I can work around it, but the API is designed in a way that it expects the const enum to be used.