typescript-go icon indicating copy to clipboard operation
typescript-go copied to clipboard

const enum is not work

Open sgjm opened this issue 7 months ago • 4 comments

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:

  • TE should not appear in the result
  • TE.A and TE.B are not replaced with 1 and 2

sgjm avatar Apr 27 '25 16:04 sgjm

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

gauravanand867 avatar May 04 '25 08:05 gauravanand867

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

Tks. the problem remains. Perhaps there's another factor.

sgjm avatar May 04 '25 09:05 sgjm

What is the current issue

gauravanand867 avatar May 04 '25 10:05 gauravanand867

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:

  • TE should not appear in the result
  • TE.A and TE.B are not replaced with 1 and 2

The output file is expected to have only 2 lines:

console.log(1);
console.log(2);

sgjm avatar May 06 '25 11:05 sgjm

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 avatar Jun 04 '25 06:06 jakebailey

@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:

Image

AlCalzone avatar Jun 17 '25 09:06 AlCalzone

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.

jakebailey avatar Jun 17 '25 17:06 jakebailey

I guess I can work around it, but the API is designed in a way that it expects the const enum to be used.

AlCalzone avatar Jun 17 '25 21:06 AlCalzone