-
Notifications
You must be signed in to change notification settings - Fork 13.2k
fix(checker): Allow element access expressions in computed property names if argument is literal #62968
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
c7b8d77 to
3784ace
Compare
…ames if argument is literal
3784ace to
e813bb0
Compare
99b7bff to
91424d8
Compare
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 fixes issue #25083 by allowing element access expressions (e.g., Enum['key']) as computed property names in type literals when the argument is a literal. Previously, only dot notation (e.g., Enum.key) was accepted for enum members in computed property names.
Changes:
- Modified
isLateBindableASTto use a new helperisLateBindableExpressionthat recursively validates element access chains with literal arguments - Updated utility functions to handle signed numeric literals in computed property names
- Added test case covering enum keys accessed via bracket notation in type literals
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 |
|---|---|
tests/cases/compiler/enumKeysInTypeLiteral.ts |
New test case demonstrating enum element access with bracket notation in type literals |
tests/baselines/reference/enumKeysInTypeLiteral.* |
Baseline files for the new test showing expected types, symbols, and JS output |
tests/baselines/reference/isolatedDeclarationLazySymbols.* |
Updated baselines reflecting improved type inference for element access expressions |
src/compiler/checker.ts |
Added isLateBindableExpression helper and modified isLateBindableAST to handle element access chains |
src/compiler/utilities.ts |
Updated isComputedNonLiteralName and tryGetTextOfPropertyName to handle signed numeric literals; formatting improvements |
| else if (isElementAccessExpression(node)) { | ||
| return isLateBindableExpression(node.argumentExpression); |
Copilot
AI
Feb 4, 2026
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.
The logic for handling ElementAccessExpression is incorrect. When node is an ElementAccessExpression (e.g., Type['key']), this code passes only node.argumentExpression (which would be the literal 'key') to isLateBindableExpression.
However, isLateBindableExpression expects to validate the full expression chain. When passed a literal like 'key', it skips the while loop (since a literal is not an ElementAccessExpression) and calls isEntityNameExpression('key'), which returns false because a string literal is not an entity name expression.
The correct approach is to pass the entire node to isLateBindableExpression, not just node.argumentExpression. This would allow the function to properly validate the element access chain and then check that the base is an entity name expression.
Fixes #25083.
This PR relaxes the check for computed property names in type literals (specifically in
isLateBindableAST) to allowElementAccessExpressionwhen the argument is a static literal.Changes
Checker (
src/compiler/checker.ts)isLateBindableASTto use a new helperisLateBindableExpression.isLateBindableExpressionrecursively checks the expression and allows:Enum['key'])Enum[-1])Enum[('key')])Utilities (
src/compiler/utilities.ts)isComputedNonLiteralNameto also check forisSignedNumericLiteraltryGetTextOfPropertyNameto handle signed numeric literals inComputedPropertyName, aligning withgetPropertyNameForPropertyNameNodeExample
Verified with new test case
tests/cases/compiler/enumKeysInTypeLiteral.tscovering these scenarios.