-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Add deprecation warning for computed property names in enums #62818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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.
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 |
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