Skip to content

Commit 1a742e6

Browse files
Merge pull request #27918 from collin5/b27914
Improve error message for duplicate property with computed name
2 parents 3249e0f + d396830 commit 1a742e6

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6534,9 +6534,9 @@ namespace ts {
65346534
// If we have an existing early-bound member, combine its declarations so that we can
65356535
// report an error at each declaration.
65366536
const declarations = earlySymbol ? concatenate(earlySymbol.declarations, lateSymbol.declarations) : lateSymbol.declarations;
6537-
const name = declarationNameToString(decl.name);
6538-
forEach(declarations, declaration => error(getNameOfDeclaration(declaration) || declaration, Diagnostics.Duplicate_declaration_0, name));
6539-
error(decl.name || decl, Diagnostics.Duplicate_declaration_0, name);
6537+
const name = (<LiteralType>type).value || declarationNameToString(decl.name);
6538+
forEach(declarations, declaration => error(getNameOfDeclaration(declaration) || declaration, Diagnostics.Property_0_was_also_declared_here, name));
6539+
error(decl.name || decl, Diagnostics.Duplicate_property_0, name);
65406540
lateSymbol = createSymbol(SymbolFlags.None, memberName, CheckFlags.Late);
65416541
}
65426542
lateSymbol.nameType = type;

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2433,7 +2433,7 @@
24332433
"category": "Error",
24342434
"code": 2717
24352435
},
2436-
"Duplicate declaration '{0}'.": {
2436+
"Duplicate property '{0}'.": {
24372437
"category": "Error",
24382438
"code": 2718
24392439
},
@@ -2493,6 +2493,10 @@
24932493
"category": "Error",
24942494
"code": 2732
24952495
},
2496+
"Property '{0}' was also declared here.": {
2497+
"category": "Error",
2498+
"code": 2733
2499+
},
24962500
"It is highly likely that you are missing a semicolon.": {
24972501
"category": "Error",
24982502
"code": 2734

tests/baselines/reference/dynamicNamesErrors.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/compiler/dynamicNamesErrors.ts(5,5): error TS2718: Duplicate declaration '[c0]'.
2-
tests/cases/compiler/dynamicNamesErrors.ts(6,5): error TS2718: Duplicate declaration '[c0]'.
1+
tests/cases/compiler/dynamicNamesErrors.ts(5,5): error TS2718: Duplicate property '1'.
2+
tests/cases/compiler/dynamicNamesErrors.ts(6,5): error TS2733: Property '1' was also declared here.
33
tests/cases/compiler/dynamicNamesErrors.ts(19,5): error TS2717: Subsequent property declarations must have the same type. Property '[c1]' must be of type 'number', but here has type 'string'.
44
tests/cases/compiler/dynamicNamesErrors.ts(24,1): error TS2322: Type 'T2' is not assignable to type 'T1'.
55
Types of property '[c0]' are incompatible.
@@ -16,10 +16,10 @@ tests/cases/compiler/dynamicNamesErrors.ts(25,1): error TS2322: Type 'T1' is not
1616
interface T0 {
1717
[c0]: number;
1818
~~~~
19-
!!! error TS2718: Duplicate declaration '[c0]'.
19+
!!! error TS2718: Duplicate property '1'.
2020
1: number;
2121
~
22-
!!! error TS2718: Duplicate declaration '[c0]'.
22+
!!! error TS2733: Property '1' was also declared here.
2323
}
2424

2525
interface T1 {

0 commit comments

Comments
 (0)