Add deprecation warning for computed property names in enums#62818
Add deprecation warning for computed property names in enums#62818magic-akari wants to merge 2 commits intomicrosoft:mainfrom
Conversation
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
|
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
|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
There was a problem hiding this comment.
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 |
|
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:
|
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:
This syntax is:
Approach
To avoid breaking existing code, this PR takes a gradual deprecation approach:
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