Skip to content

Add deprecation warning for computed property names in enums#62818

Closed
magic-akari wants to merge 2 commits intomicrosoft:mainfrom
magic-akari:fix/issue-42468
Closed

Add deprecation warning for computed property names in enums#62818
magic-akari wants to merge 2 commits intomicrosoft:mainfrom
magic-akari:fix/issue-42468

Conversation

@magic-akari
Copy link
Copy Markdown
Contributor

@magic-akari magic-akari commented Dec 1, 2025

Summary

Add a deprecation warning for computed property names in enum members (e.g., ["key"] or [`key`]), along with a quick fix to convert them to simple string literals.

Background

As discussed in #42468 and #62649, TypeScript currently allows computed property names with literal expressions in enums:

enum CHAR {
    ['\t'] = 0x09,  // allowed, but semantically equivalent to '\t' = 0x09
    [`\r`] = 0x0D,
}

This syntax is:

  • Rarely used in practice
  • Not supported by Babel or typescript-eslint
  • Semantically redundant — only literal expressions are allowed, so ['key'] is always equivalent to 'key'

Approach

To avoid breaking existing code, this PR takes a gradual deprecation approach:

  1. Phase 1 (this PR): Add a suggestion diagnostic marking this syntax as deprecated, with an auto-fix
  2. Phase 2 (future): Disallow this syntax entirely

This aligns with Anders' comment in #42468:
I wouldn't be opposed to disallowing computed property names in enums altogether... That said, I hate to break stuff.

Changes

  • Add suggestion diagnostic (code 1550) for literal computed property names in enums
  • Add quick fix: "Remove unnecessary computed property name syntax"
  • Add "Fix All" support for batch conversion

This adds a suggestion diagnostic for using computed property names with
literal expressions in enum members (e.g., `["key"]`).

This syntax is rarely used, not supported by Babel/typescript-eslint/SWC,
and will be disallowed in a future version (typescript-go).

Changes:
- Add suggestion diagnostic (code 1550) for computed property names
- Add quick fix to convert `["key"]` to `"key"`
- Add "Fix All" support for batch conversion
Copilot AI review requested due to automatic review settings December 1, 2025 05:36
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Dec 1, 2025
@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Dec 1, 2025
@typescript-bot
Copy link
Copy Markdown
Collaborator

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

1 similar comment
@typescript-bot
Copy link
Copy Markdown
Collaborator

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

@typescript-bot typescript-bot added For Backlog Bug PRs that fix a backlog bug and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Dec 1, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements a gradual deprecation strategy for computed property names with literal expressions in enum members (e.g., ['\t'], [`\r`]). The approach adds a suggestion diagnostic (code 1550) with an automatic fix, preparing for eventual removal of this syntax while avoiding breaking existing code immediately.

Key changes:

  • Added deprecation suggestion diagnostic for literal computed property names in enums
  • Implemented quick fix to convert computed property syntax to simple string literals
  • Added comprehensive fourslash tests for errors, suggestions, and code fixes

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/compiler/diagnosticMessages.json Adds diagnostic message 1550 for deprecation warning and messages 95198/95199 for codefix descriptions
src/compiler/checker.ts Adds suggestion diagnostic in computeEnumMemberValue when computed property names are detected
src/services/codefixes/convertComputedEnumMemberName.ts Implements the codefix to convert ['\t']"\t" style transformations with fix-all support
src/services/_namespaces/ts.codefix.ts Registers the new codefix module
tests/cases/fourslash/enumComputedPropertyNameError.ts Tests that non-literal computed properties get error 1164
tests/cases/fourslash/enumComputedPropertyNameDeprecated.ts Tests that literal computed properties get suggestion 1550
tests/cases/fourslash/codeFixEnumComputedPropertyName.ts Tests single instance code fix application
tests/cases/fourslash/codeFixEnumComputedPropertyNameAll.ts Tests fix-all functionality across multiple instances

@typescript-bot
Copy link
Copy Markdown
Collaborator

With 6.0 out as the final release vehicle for this codebase, we're closing all PRs that don't fit the merge criteria for post-6.0 patches. If you think this was a mistake and this PR fits the post-6.0 patch criteria, please post to the 6.0 iteration issue with details (specifically, which PR and which patch criteria it satisfies).

Next steps for PRs:

  • For crash bugfixes or language service improvements, PRs are currently accepted at the typescript-go repo
  • Changes to type system behavior should wait until after 7.0, at which point mainline TypeScript development will resume in this repository with the Go codebase
  • Library file updates (lib.d.ts etc) continue to live in this repo or the DOM Generator repo as appropriate

@github-project-automation github-project-automation bot moved this from Not started to Done in PR Backlog Mar 24, 2026
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

Status: Done

Development

Successfully merging this pull request may close these issues.

Enum declarations should consistently disallow computed keys

4 participants