Skip to content

Commit c7b8d77

Browse files
committed
Fix(checker): Allow element access expressions in computed property names if argument is literal (Fixes #25083)
1 parent c574e40 commit c7b8d77

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/compiler/checker.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13720,14 +13720,24 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1372013720
&& isTypeUsableAsIndexSignature(isComputedPropertyName(node) ? checkComputedPropertyName(node) : checkExpressionCached((node as ElementAccessExpression).argumentExpression));
1372113721
}
1372213722

13723-
function isLateBindableAST(node: DeclarationName) {
13724-
if (!isComputedPropertyName(node) && !isElementAccessExpression(node)) {
13725-
return false;
13723+
function isLateBindableExpression(expr: Expression): boolean {
13724+
while (isElementAccessExpression(expr)) {
13725+
if (!isStringOrNumericLiteralLike(expr.argumentExpression)) return false;
13726+
expr = expr.expression;
1372613727
}
13727-
const expr = isComputedPropertyName(node) ? node.expression : node.argumentExpression;
1372813728
return isEntityNameExpression(expr);
1372913729
}
1373013730

13731+
function isLateBindableAST(node: DeclarationName) {
13732+
if (isComputedPropertyName(node)) {
13733+
return isLateBindableExpression(node.expression);
13734+
}
13735+
else if (isElementAccessExpression(node)) {
13736+
return isLateBindableExpression(node.argumentExpression);
13737+
}
13738+
return false;
13739+
}
13740+
1373113741
function isTypeUsableAsIndexSignature(type: Type): boolean {
1373213742
return isTypeAssignableTo(type, stringNumberSymbolType);
1373313743
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
enum Type {
3+
Foo = 'foo',
4+
'3x14' = '3x14'
5+
}
6+
7+
type TypeMap = {
8+
[Type.Foo]: 1;
9+
[Type['3x14']]: 2;
10+
}
11+
12+
const t: TypeMap = {
13+
'foo': 1,
14+
'3x14': 2
15+
};

0 commit comments

Comments
 (0)