Skip to content

TypeScript enum transform does not treat const string variables as compile-time constants #11715

@kdy1

Description

@kdy1

Describe the bug

SWC does not treat a local const string variable used in a TypeScript enum initializer as a compile-time constant.

For the input below, TypeScript 5.9 emits a string enum shape (E["A"] = "aThisIs"), but SWC emits reverse-mapping style output (E[E["A"] = foo] = "A"), which changes runtime behavior and enum shape.

Input code

const foo = 'aThisIs'

enum E {
  A = foo,
  B = 'bThisIs'
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript"
    }
  }
}

Link to the code that reproduces this issue

nodejs/amaro#394

SWC Info output

$ npx -y -p @swc/cli@latest -p @swc/core@latest swc -V
@swc/cli: 0.8.0
@swc/core: 1.15.18

Expected behavior

SWC should match TypeScript enum lowering semantics for this case (string enum, no reverse mapping for A), e.g.:

"use strict";
const foo = 'aThisIs';
var E;
(function (E) {
    E["A"] = "aThisIs";
    E["B"] = "bThisIs";
})(E || (E = {}));

Actual behavior

SWC currently emits:

var foo = 'aThisIs';
var E = function(E) {
    E[E["A"] = foo] = "A";
    E["B"] = "bThisIs";
    return E;
}(E || {});

Version

@swc/core 1.15.18 (@swc/cli 0.8.0)

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions