Skip to content

const enum is not work #829

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

Open
sgjm opened this issue Apr 27, 2025 · 4 comments
Open

const enum is not work #829

sgjm opened this issue Apr 27, 2025 · 4 comments

Comments

@sgjm
Copy link

sgjm commented Apr 27, 2025

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);

it seems compile option "preserveConstEnums": false is not work!

@gauravanand867
Copy link

gauravanand867 commented May 4, 2025

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

@sgjm
Copy link
Author

sgjm commented May 4, 2025

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.

@gauravanand867
Copy link

What is the current issue

@sgjm
Copy link
Author

sgjm commented May 6, 2025

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants