fix: allow bracket-notation enum access as computed property name in type positions#63434
fix: allow bracket-notation enum access as computed property name in type positions#63434Scolliq wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…type positions Extends isLateBindableAST to recognise element access expressions of the form Enum['non-identifier-key'] (entity name indexed by a string/numeric literal) as late-bindable, in addition to the previously-supported Enum.Foo property-access style. Previously, enum members whose names are not valid identifiers (e.g. E['hello world'], E['3x14']) could not be used as computed property keys in type literals or interfaces, even though the expression evaluates to a well-known string literal type and the error message explicitly says that literal types are allowed. Fixes microsoft#25083
|
@microsoft-github-policy-service agree |
|
Can you please tell me what tool you used to send this PR, and how you chose this issue to fix? |
I used Claude Code to help scan large open-source codebases for unfixed issues. It surfaced this one, and I went through the root cause with the help of the opus 4.7 model tracing isLateBindableAST and understanding why element access expressions on entity names weren't being treated as late-bindable the same way property-access ones are. I study computer science, this isnt just a blind purely vibecoded small commit. I have also read the typescript repos rules on AI tools in the CONTRIBUTING.md |
|
You said:
But the section of CONTRIBUTING.md that you're referring to says:
Which you didn't do. CONTRIBUTING.md also says:
|
Problem
Enum members with non-identifier names (e.g.
'hello world','3x14') could not be used as computed property keys in type literals or interfaces using bracket notation, even though the expression evaluates to a well-known string literal type.The error message says "must refer to an expression whose type is a literal type" — but
E["hello world"]IS a string literal type. The check was incorrectly using expression shape (entity name expression) as a proxy for the type.Fixes #25083.
Root Cause
isLateBindableASTonly recognisedEntityNameExpressionforms (identifiers and property-access chains likeA.B.C) as potentially late-bindable. Element-access expressions likeE["hello world"]were unconditionally rejected, makingisNonBindableDynamicNamereturntrueand triggering the grammar error.Fix
Extended
isLateBindableASTto also returntruewhen the expression is anElementAccessExpressionwhose object is an entity name and whose argument is a string or numeric literal:isLateBindableNamealready checksisTypeUsableAsPropertyNameon the resolved type, so this only opens the door for expressions that actually evaluate to a usable property name type (string/number literal or unique symbol).Tests
Added
tests/cases/compiler/enumBracketComputedPropertyName.tscovering:E["hello world"],E["3x14"])Updated
isolatedDeclarationLazySymbolsbaselines to reflect improved behaviour:TS1166no longer fires for[o["prop.inner"]]in a class (correct — the type is a literal), andfoo[o["prop.inner"]]now correctly resolves tostringinstead ofany.