You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enum declarations should consistently disallow computed keysย #42468
TS reports Computed property names are not allowed in enums. for [{}], [y] and [z], while it allows ['x'] (in contraddiction with the error message).
๐ Expected behavior
All of them should be an error, for the reason reported by the error.
๐ Context
This came up at babel/babel#12683: we can implement the current TS behavior in Babel, but I'd first like to check if TS is expected to report an error for ['x'].
The error message is underspecific; you can write [e] if e is exactly a numeric literal, string literal, or template literal with no holes.
I don't know why we added that logic ๐
To support keys containing special symbols? IMO, Dynamic property names are not allowed in enums. might be better.
With typescript-go on the horizon, I wanted to revisit this issue and propose a clear migration path.
Current State
This feature has extremely limited ecosystem support (ref):
Babel, typescript-eslint, and SWC: None implement this syntax
Real-world usage: Extremely rare (only sporadic bug reports despite years without support in these tools)
The bracket form is also semantically redundantโsince only literal expressions are allowed (no [x] or ["a" + "b"]), ['\\t'] can always be replaced with '\\t'.
Proposal
Phase 1 (TypeScript): Add a deprecation warning with auto-fix
enumCHAR{['\\t']=0x09,// โ ๏ธ Computed property names are not allowed in enums// ๐ก Quick fix: Replace with '\\t' = 0x09}
Phase 2 (typescript-go): Completely disallow bracket-style enum members
I wouldn't be opposed to disallowing computed property names in enums altogether, particularly since that's the intent our error message already conveys. Agree with Ryan Cavanaugh (@RyanCavanaugh)here, not sure why we permit it. That said, I hate to break stuff and I have no idea what the impact of disallowing it would be.
Activity
RyanCavanaugh commentedon Jan 26, 2021
The error message is underspecific; you can write
[e]ifeis exactly a numeric literal, string literal, or template literal with no holes.I don't know why we added that logic ๐
armano2 commentedon Jan 26, 2021
it works only for "template literals without holes" and string literals
RyanCavanaugh commentedon Jan 26, 2021
My prior comment was underspecific; you will not get that error message if the name is numeric ๐
Zzzen commentedon Jul 12, 2021
To support keys containing special symbols? IMO,
Dynamic property names are not allowed in enums.might be better.add suggestion diagnostic for enum computed string keys (microsoft#42468
magic-akari commentedon Oct 22, 2025
With typescript-go on the horizon, I wanted to revisit this issue and propose a clear migration path.
Current State
This feature has extremely limited ecosystem support (ref):
The bracket form is also semantically redundantโsince only literal expressions are allowed (no
[x]or["a" + "b"]),['\\t']can always be replaced with'\\t'.Proposal
Phase 1 (TypeScript): Add a deprecation warning with auto-fix
Phase 2 (typescript-go): Completely disallow bracket-style enum members
This aligns with:
Impact
Minimalโthe syntax is rarely used and can be mechanically migrated via auto-fix.
Would the team be open to this approach? Happy to help with implementation if there's interest.
ahejlsberg commentedon Oct 22, 2025
I wouldn't be opposed to disallowing computed property names in enums altogether, particularly since that's the intent our error message already conveys. Agree with Ryan Cavanaugh (@RyanCavanaugh) here, not sure why we permit it. That said, I hate to break stuff and I have no idea what the impact of disallowing it would be.
Deprecate literal computed property names in enums
Deprecate literal computed property names in enums