Allow enum keys accessed with bracket notation as computed properties#62892
Open
tt-a1i wants to merge 5 commits intomicrosoft:mainfrom
Open
Allow enum keys accessed with bracket notation as computed properties#62892tt-a1i wants to merge 5 commits intomicrosoft:mainfrom
tt-a1i wants to merge 5 commits intomicrosoft:mainfrom
Conversation
This fixes microsoft#25083 where enum keys accessed with bracket notation (e.g., `Type['3x14']`) were not recognized as valid computed property names in type literals, even when they resolved to literal types. The fix extends `isLateBindableAST` to recognize `ElementAccessExpression` with string or numeric literal keys as valid late-bindable expressions, similar to how `PropertyAccessExpression` is already handled. This also updates `checkGrammarForInvalidDynamicName` to allow such expressions as computed property names.
Contributor
Author
|
@microsoft-github-policy-service agree |
- Rename isEntityNameOrElementAccessExpression to isLateBindableAccessExpression - Support mixed chains like obj.a['b'].c['d'] - Add skipParentheses handling for expressions like (obj.a)['b'] - Ensure PropertyAccessExpression name is Identifier (not PrivateIdentifier) - Add comprehensive test cases for mixed chains and parenthesized expressions
Support obj[('a')] by applying skipParentheses to argumentExpression
before checking isStringOrNumericLiteralLike.
Added test cases for parenthesized keys in element access.
- Fix declaration emit elide condition to recognize ElementAccessExpression as a valid late-bindable expression (declarations.ts:1026) - Update isolatedDeclarations check to accept late-bindable access expressions (declarations.ts:1019) - Add LateBindableAccessExpression type alias for clearer semantics - Extract isLateBindableAccessExpression to utilities.ts and remove duplicate implementation from checker.ts - Update getFirstIdentifier to support ElementAccessExpression chains - Update isEntityNameVisible and related APIs to accept ElementAccessExpression - Add test case with @declaration: true to verify computed properties are preserved in generated .d.ts files
- Add reference baselines for enumKeysExportScenario test - Fix getFirstIdentifier to skipParentheses when traversing access chains to handle cases like (obj.a)['b']
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes #25083 where enum keys accessed with bracket notation (e.g.,
Type['3x14']) were not recognized as valid computed property names in type literals, even when they resolved to literal types.The problem
The error occurred because
isLateBindableASTonly recognizedEntityNameExpression(Identifier or PropertyAccessExpression) but notElementAccessExpressionwith literal keys.The fix
Introduced
isLateBindableAccessExpression, a helper function that recursively validates access expressions:skipParenthesesThis supports:
obj.a.b.cobj['a']['b']['c']obj.a['b'].c['d'](obj.a)['b']obj[('a')]Also updated
checkGrammarForInvalidDynamicNameto use the new helper (it handlesskipParenthesesinternally now).Testing
Added test case
enumKeysAsComputedPropertiesWithBracketNotation.tscovering:Type['3x14'])Type['Foo'])obj['a']['b'])obj['a'].b,obj.a['b'])deep.a['b'].c['d'])(obj.a).b,(obj['a']).b)obj[('a')],deep[('a')][('b')].c['d'])Baseline changes
enumKeysAsComputedPropertiesWithBracketNotation.*
isolatedDeclarationLazySymbols.errors.txt
Direction['Up']in[Direction['Up']]: numberDirection['Up']wasn't recognized as a valid late-bindable name"UP"isolatedDeclarationLazySymbols.types
[Direction['Up']]now correctly resolves to"UP"instead of showingerrorAll 99,161 tests pass.