Skip to content

Deprecate literal computed property names in enums - #64358

Open
magic-akari wants to merge 2 commits into
microsoft:mainfrom
magic-akari:fix/enum-computed-property-deprecation
Open

magic-akari wants to merge 2 commits into
microsoft:mainfrom
magic-akari:fix/enum-computed-property-deprecation

Conversation

@magic-akari

Copy link
Copy Markdown
Contributor

TypeScript already reports “Computed property names are not allowed in enums.” However, it makes an exception for string literals and template literals without substitutions. As Anders noted in the issue, disallowing computed names altogether would match the intent of the existing diagnostic, but compatibility is a concern.

The accepted forms do not add expressive power: single-quoted strings, double-quoted strings, and template literals without substitutions can all be written as ordinary string literal member names. References to variables and expressions such as ["a" + "b"] are already rejected.

For example:

enum Example {
    ['first'],
    ["second"],
    [`third`],
}

// Equivalent string literal names:
enum Example {
    'first',
    "second",
    "third",
}

This PR provides a gradual migration path without introducing new compilation errors. It adds a deprecation suggestion for the accepted computed names and a quick fix to convert them to ordinary string literals. Individual fixes, Fix All, and source.fixAll are supported, with decoded member names preserved.

Names that already produce errors retain their existing diagnostics without an additional deprecation suggestion. Tests cover these cases, individual and batch fixes, and escaped and multiline literal contents.

Report a deprecation suggestion for enum members named with computed
string literals or template literals without substitutions. Preserve
existing compiler behavior and avoid adding suggestions to names that
already produce errors.

Add a quick fix to replace the computed name with a plain string literal,
with support for fix-all quick fixes and source.fixAll. Preserve the
decoded member name, including escapes and multiline template contents.

Add regression coverage for diagnostics, individual and batch fixes,
and invalid dynamic or numeric names.

Refs microsoft#42468
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Sep 20, 2026
@magic-akari
magic-akari marked this pull request as ready for review September 20, 2026 05:35
Copilot AI balanced review requested due to automatic review settings September 20, 2026 05:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The code fix can silently delete comments contained inside computed property brackets.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Adds a gradual deprecation path for literal computed enum member names and an automated conversion fix.

Changes:

  • Reports deprecated computed string/template enum names.
  • Adds individual, Fix All, and source.fixAll conversions.
  • Adds diagnostic and code-fix coverage.
File Description
tsc/​internal/​ls/​codeactions.go Registers the new code-fix provider.
tsc/​internal/​ls/​codeactions_convertcomputedenummembername.go Implements computed-name conversions.
tsc/​internal/​fourslash/​tests/​enumComputedPropertyNameError_test.go Verifies existing errors remain unchanged.
tsc/​internal/​fourslash/​tests/​enumComputedPropertyNameDeprecated_test.go Verifies deprecation suggestions.
tsc/​internal/​fourslash/​tests/​codeFixEnumComputedPropertyNameAll_test.go Tests batch fixes.
tsc/​internal/​fourslash/​tests/​codeFixEnumComputedPropertyName_test.go Tests individual fixes and escaping.
tsc/​internal/​diagnostics/​diagnostics_generated.go Adds generated diagnostic definitions.
tsc/​internal/​diagnostics/​diagnosticMessages.json Defines new diagnostic messages.
tsc/​internal/​checker/​checker.go Emits the deprecation suggestion.
Files not reviewed (1)
  • tsc/internal/diagnostics/diagnostics_generated.go: Generated file

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread tsc/internal/ls/codeactions_convertcomputedenummembername.go

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The implementation matches the stated migration behavior and includes comprehensive focused coverage.

Review effort: Balanced
Findings: None

Resolved since last review (1)
Files not reviewed (1)
  • tsc/internal/diagnostics/diagnostics_generated.go: Generated file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enum declarations should consistently disallow computed keys

2 participants