Skip to content

Commit ac58751

Browse files
committed
Object literals computed property names allow literal-typed expressions
1 parent c82881f commit ac58751

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13526,6 +13526,7 @@ namespace ts {
1352613526
for (let i = 0; i < node.properties.length; i++) {
1352713527
const memberDecl = node.properties[i];
1352813528
let member = memberDecl.symbol;
13529+
let literalName: __String | undefined;
1352913530
if (memberDecl.kind === SyntaxKind.PropertyAssignment ||
1353013531
memberDecl.kind === SyntaxKind.ShorthandPropertyAssignment ||
1353113532
isObjectLiteralMethod(memberDecl)) {
@@ -13536,6 +13537,12 @@ namespace ts {
1353613537

1353713538
let type: Type;
1353813539
if (memberDecl.kind === SyntaxKind.PropertyAssignment) {
13540+
if (memberDecl.name.kind === SyntaxKind.ComputedPropertyName) {
13541+
const t = checkComputedPropertyName(<ComputedPropertyName>memberDecl.name);
13542+
if (t.flags & TypeFlags.Literal) {
13543+
literalName = escapeLeadingUnderscores("" + (t as LiteralType).value);
13544+
}
13545+
}
1353913546
type = checkPropertyAssignment(<PropertyAssignment>memberDecl, checkMode);
1354013547
}
1354113548
else if (memberDecl.kind === SyntaxKind.MethodDeclaration) {
@@ -13552,7 +13559,7 @@ namespace ts {
1355213559
}
1355313560

1355413561
typeFlags |= type.flags;
13555-
const prop = createSymbol(SymbolFlags.Property | member.flags, member.escapedName);
13562+
const prop = createSymbol(SymbolFlags.Property | member.flags, literalName || member.escapedName);
1355613563
if (inDestructuringPattern) {
1355713564
// If object literal is an assignment pattern and if the assignment pattern specifies a default value
1355813565
// for the property, make the property optional.
@@ -13562,7 +13569,7 @@ namespace ts {
1356213569
if (isOptional) {
1356313570
prop.flags |= SymbolFlags.Optional;
1356413571
}
13565-
if (hasDynamicName(memberDecl)) {
13572+
if (!literalName && hasDynamicName(memberDecl)) {
1356613573
patternWithComputedProperties = true;
1356713574
}
1356813575
}
@@ -13620,7 +13627,7 @@ namespace ts {
1362013627
checkNodeDeferred(memberDecl);
1362113628
}
1362213629

13623-
if (hasDynamicName(memberDecl)) {
13630+
if (!literalName && hasDynamicName(memberDecl)) {
1362413631
if (isNumericName(memberDecl.name)) {
1362513632
hasComputedNumberProperty = true;
1362613633
}

0 commit comments

Comments
 (0)