Skip to content

"No repeated property names" error in object literals is duplicated in strict mode #46929

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

Merged
merged 2 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2066,12 +2066,6 @@ namespace ts {
seen.set(identifier.escapedText, currentKind);
continue;
}

if (currentKind === ElementKind.Property && existingKind === ElementKind.Property) {
const span = getErrorSpanForNode(file, identifier);
file.bindDiagnostics.push(createFileDiagnostic(file, span.start, span.length,
Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode));
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43126,9 +43126,12 @@ namespace ts {
seen.set(effectiveName, currentKind);
}
else {
if ((currentKind & DeclarationMeaning.PropertyAssignmentOrMethod) && (existingKind & DeclarationMeaning.PropertyAssignmentOrMethod)) {
if ((currentKind & DeclarationMeaning.Method) && (existingKind & DeclarationMeaning.Method)) {
grammarErrorOnNode(name, Diagnostics.Duplicate_identifier_0, getTextOfNode(name));
}
else if ((currentKind & DeclarationMeaning.PropertyAssignment) && (existingKind & DeclarationMeaning.PropertyAssignment)) {
grammarErrorOnNode(name, Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name, getTextOfNode(name));
}
else if ((currentKind & DeclarationMeaning.GetOrSetAccessor) && (existingKind & DeclarationMeaning.GetOrSetAccessor)) {
if (existingKind !== DeclarationMeaning.GetOrSetAccessor && currentKind !== existingKind) {
seen.set(effectiveName, currentKind | existingKind);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@
"category": "Error",
"code": 1116
},
"An object literal cannot have multiple properties with the same name in strict mode.": {
"An object literal cannot have multiple properties with the same name.": {
"category": "Error",
"code": 1117
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/compiler/duplicateIdentifierDifferentSpelling.ts(3,3): error TS2300: Duplicate identifier '3'.
tests/cases/compiler/duplicateIdentifierDifferentSpelling.ts(6,21): error TS2300: Duplicate identifier '3'.
tests/cases/compiler/duplicateIdentifierDifferentSpelling.ts(6,21): error TS1117: An object literal cannot have multiple properties with the same name.


==== tests/cases/compiler/duplicateIdentifierDifferentSpelling.ts (2 errors) ====
Expand All @@ -12,5 +12,5 @@ tests/cases/compiler/duplicateIdentifierDifferentSpelling.ts(6,21): error TS2300

var X = { 0b11: '', 3: '' };
~
!!! error TS2300: Duplicate identifier '3'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/compiler/duplicateObjectLiteralProperty.ts(4,5): error TS2300: Duplicate identifier 'a'.
tests/cases/compiler/duplicateObjectLiteralProperty.ts(5,5): error TS2300: Duplicate identifier '\u0061'.
tests/cases/compiler/duplicateObjectLiteralProperty.ts(6,5): error TS2300: Duplicate identifier 'a'.
tests/cases/compiler/duplicateObjectLiteralProperty.ts(8,9): error TS2300: Duplicate identifier '"c"'.
tests/cases/compiler/duplicateObjectLiteralProperty.ts(4,5): error TS1117: An object literal cannot have multiple properties with the same name.
tests/cases/compiler/duplicateObjectLiteralProperty.ts(5,5): error TS1117: An object literal cannot have multiple properties with the same name.
tests/cases/compiler/duplicateObjectLiteralProperty.ts(6,5): error TS1117: An object literal cannot have multiple properties with the same name.
tests/cases/compiler/duplicateObjectLiteralProperty.ts(8,9): error TS1117: An object literal cannot have multiple properties with the same name.
tests/cases/compiler/duplicateObjectLiteralProperty.ts(14,9): error TS2300: Duplicate identifier 'a'.
tests/cases/compiler/duplicateObjectLiteralProperty.ts(15,9): error TS2300: Duplicate identifier 'a'.
tests/cases/compiler/duplicateObjectLiteralProperty.ts(16,9): error TS1118: An object literal cannot have multiple get/set accessors with the same name.
Expand All @@ -14,17 +14,17 @@ tests/cases/compiler/duplicateObjectLiteralProperty.ts(16,9): error TS2300: Dupl
b: true, // OK
a: 56, // Duplicate
~
!!! error TS2300: Duplicate identifier 'a'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
\u0061: "ss", // Duplicate
~~~~~~
!!! error TS2300: Duplicate identifier '\u0061'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
a: {
~
!!! error TS2300: Duplicate identifier 'a'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
c: 1,
"c": 56, // Duplicate
~~~
!!! error TS2300: Duplicate identifier '"c"'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(3,5): error TS2300: Duplicate identifier '[1]'.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(8,5): error TS2300: Duplicate identifier '[+1]'.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(13,5): error TS2300: Duplicate identifier '[+1]'.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(23,5): error TS2300: Duplicate identifier '["+1"]'.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(28,5): error TS2300: Duplicate identifier '[-1]'.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(33,5): error TS2300: Duplicate identifier '["-1"]'.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(3,5): error TS1117: An object literal cannot have multiple properties with the same name.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(8,5): error TS1117: An object literal cannot have multiple properties with the same name.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(13,5): error TS1117: An object literal cannot have multiple properties with the same name.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(23,5): error TS1117: An object literal cannot have multiple properties with the same name.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(28,5): error TS1117: An object literal cannot have multiple properties with the same name.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(33,5): error TS1117: An object literal cannot have multiple properties with the same name.


==== tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts (6 errors) ====
const t1 = {
1: 1,
[1]: 0 // duplicate
~~~
!!! error TS2300: Duplicate identifier '[1]'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
}

const t2 = {
1: 1,
[+1]: 0 // duplicate
~~~~
!!! error TS2300: Duplicate identifier '[+1]'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
}

const t3 = {
"1": 1,
[+1]: 0 // duplicate
~~~~
!!! error TS2300: Duplicate identifier '[+1]'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
}

const t4 = {
Expand All @@ -37,20 +37,20 @@ tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(33,5): error
"+1": 1,
["+1"]: 0 // duplicate
~~~~~~
!!! error TS2300: Duplicate identifier '["+1"]'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
}

const t6 = {
"-1": 1,
[-1]: 0 // duplicate
~~~~
!!! error TS2300: Duplicate identifier '[-1]'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
}

const t7 = {
"-1": 1,
["-1"]: 0 // duplicate
~~~~~~
!!! error TS2300: Duplicate identifier '["-1"]'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
}

Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
tests/cases/compiler/duplicatePropertiesInStrictMode.ts(4,3): error TS1117: An object literal cannot have multiple properties with the same name in strict mode.
tests/cases/compiler/duplicatePropertiesInStrictMode.ts(4,3): error TS2300: Duplicate identifier 'x'.
tests/cases/compiler/duplicatePropertiesInStrictMode.ts(4,3): error TS1117: An object literal cannot have multiple properties with the same name.


==== tests/cases/compiler/duplicatePropertiesInStrictMode.ts (2 errors) ====
==== tests/cases/compiler/duplicatePropertiesInStrictMode.ts (1 errors) ====
"use strict";
var x = {
x: 1,
x: 2
~
!!! error TS1117: An object literal cannot have multiple properties with the same name in strict mode.
~
!!! error TS2300: Duplicate identifier 'x'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
}
8 changes: 4 additions & 4 deletions tests/baselines/reference/duplicatePropertyNames.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ tests/cases/conformance/types/members/duplicatePropertyNames.ts(35,5): error TS2
tests/cases/conformance/types/members/duplicatePropertyNames.ts(36,5): error TS2300: Duplicate identifier 'foo'.
tests/cases/conformance/types/members/duplicatePropertyNames.ts(38,5): error TS2300: Duplicate identifier 'bar'.
tests/cases/conformance/types/members/duplicatePropertyNames.ts(39,5): error TS2300: Duplicate identifier 'bar'.
tests/cases/conformance/types/members/duplicatePropertyNames.ts(44,5): error TS2300: Duplicate identifier 'foo'.
tests/cases/conformance/types/members/duplicatePropertyNames.ts(46,5): error TS2300: Duplicate identifier 'bar'.
tests/cases/conformance/types/members/duplicatePropertyNames.ts(44,5): error TS1117: An object literal cannot have multiple properties with the same name.
tests/cases/conformance/types/members/duplicatePropertyNames.ts(46,5): error TS1117: An object literal cannot have multiple properties with the same name.


==== tests/cases/conformance/types/members/duplicatePropertyNames.ts (16 errors) ====
Expand Down Expand Up @@ -90,10 +90,10 @@ tests/cases/conformance/types/members/duplicatePropertyNames.ts(46,5): error TS2
foo: '',
foo: '',
~~~
!!! error TS2300: Duplicate identifier 'foo'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
bar: () => { },
bar: () => { }
~~~
!!! error TS2300: Duplicate identifier 'bar'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
tests/cases/compiler/a.js(5,5): error TS1117: An object literal cannot have multiple properties with the same name in strict mode.
tests/cases/compiler/a.js(5,5): error TS2300: Duplicate identifier 'a'.
tests/cases/compiler/a.js(5,5): error TS1117: An object literal cannot have multiple properties with the same name.
tests/cases/compiler/a.js(7,5): error TS1212: Identifier expected. 'let' is a reserved word in strict mode.
tests/cases/compiler/a.js(8,8): error TS1102: 'delete' cannot be called on an identifier in strict mode.
tests/cases/compiler/a.js(8,8): error TS2703: The operand of a 'delete' operator must be a property reference.
Expand All @@ -15,16 +14,14 @@ tests/cases/compiler/d.js(2,9): error TS1121: Octal literals are not allowed in
tests/cases/compiler/d.js(2,11): error TS1005: ',' expected.


==== tests/cases/compiler/a.js (9 errors) ====
==== tests/cases/compiler/a.js (8 errors) ====
"use strict";
var a = {
a: "hello", // error
b: 10,
a: 10 // error
~
!!! error TS1117: An object literal cannot have multiple properties with the same name in strict mode.
~
!!! error TS2300: Duplicate identifier 'a'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
};
var let = 10; // error
~~~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
tests/cases/compiler/lastPropertyInLiteralWins.ts(8,5): error TS2322: Type '(num: number) => void' is not assignable to type '(str: string) => void'.
Types of parameters 'num' and 'str' are incompatible.
Type 'string' is not assignable to type 'number'.
tests/cases/compiler/lastPropertyInLiteralWins.ts(9,5): error TS2300: Duplicate identifier 'thunk'.
tests/cases/compiler/lastPropertyInLiteralWins.ts(9,5): error TS1117: An object literal cannot have multiple properties with the same name.
tests/cases/compiler/lastPropertyInLiteralWins.ts(9,5): error TS2322: Type '(num: number) => void' is not assignable to type '(str: string) => void'.
tests/cases/compiler/lastPropertyInLiteralWins.ts(14,5): error TS2300: Duplicate identifier 'thunk'.
tests/cases/compiler/lastPropertyInLiteralWins.ts(14,5): error TS1117: An object literal cannot have multiple properties with the same name.


==== tests/cases/compiler/lastPropertyInLiteralWins.ts (4 errors) ====
Expand All @@ -22,7 +22,7 @@ tests/cases/compiler/lastPropertyInLiteralWins.ts(14,5): error TS2300: Duplicate
!!! related TS6500 tests/cases/compiler/lastPropertyInLiteralWins.ts:2:5: The expected type comes from property 'thunk' which is declared here on type 'Thing'
thunk: (num: number) => {}
~~~~~
!!! error TS2300: Duplicate identifier 'thunk'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
~~~~~
!!! error TS2322: Type '(num: number) => void' is not assignable to type '(str: string) => void'.
!!! related TS6500 tests/cases/compiler/lastPropertyInLiteralWins.ts:2:5: The expected type comes from property 'thunk' which is declared here on type 'Thing'
Expand All @@ -32,6 +32,6 @@ tests/cases/compiler/lastPropertyInLiteralWins.ts(14,5): error TS2300: Duplicate
thunk: (num: number) => {},
thunk: (str: string) => {}
~~~~~
!!! error TS2300: Duplicate identifier 'thunk'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
});

4 changes: 2 additions & 2 deletions tests/baselines/reference/memberOverride.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/memberOverride.ts(5,5): error TS2300: Duplicate identifier 'a'.
tests/cases/compiler/memberOverride.ts(5,5): error TS1117: An object literal cannot have multiple properties with the same name.


==== tests/cases/compiler/memberOverride.ts (1 errors) ====
Expand All @@ -8,7 +8,7 @@ tests/cases/compiler/memberOverride.ts(5,5): error TS2300: Duplicate identifier
a: "",
a: 5
~
!!! error TS2300: Duplicate identifier 'a'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
}

var n: number = x.a;
18 changes: 18 additions & 0 deletions tests/baselines/reference/noRepeatedPropertyNames.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
tests/cases/compiler/noRepeatedPropertyNames.ts(2,23): error TS1117: An object literal cannot have multiple properties with the same name.
tests/cases/compiler/noRepeatedPropertyNames.ts(5,32): error TS1117: An object literal cannot have multiple properties with the same name.


==== tests/cases/compiler/noRepeatedPropertyNames.ts (2 errors) ====
// https://github.com/microsoft/TypeScript/issues/46815
const first = { a: 1, a: 2 };
~
!!! error TS1117: An object literal cannot have multiple properties with the same name.
class C {
m() {
const second = { a: 1, a: 2 };
~
!!! error TS1117: An object literal cannot have multiple properties with the same name.
return second.a;
}
}

23 changes: 23 additions & 0 deletions tests/baselines/reference/noRepeatedPropertyNames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [noRepeatedPropertyNames.ts]
// https://github.com/microsoft/TypeScript/issues/46815
const first = { a: 1, a: 2 };
class C {
m() {
const second = { a: 1, a: 2 };
return second.a;
}
}


//// [noRepeatedPropertyNames.js]
// https://github.com/microsoft/TypeScript/issues/46815
var first = { a: 1, a: 2 };
var C = /** @class */ (function () {
function C() {
}
C.prototype.m = function () {
var second = { a: 1, a: 2 };
return second.a;
};
return C;
}());
25 changes: 25 additions & 0 deletions tests/baselines/reference/noRepeatedPropertyNames.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/compiler/noRepeatedPropertyNames.ts ===
// https://github.com/microsoft/TypeScript/issues/46815
const first = { a: 1, a: 2 };
>first : Symbol(first, Decl(noRepeatedPropertyNames.ts, 1, 5))
>a : Symbol(a, Decl(noRepeatedPropertyNames.ts, 1, 15), Decl(noRepeatedPropertyNames.ts, 1, 21))
>a : Symbol(a, Decl(noRepeatedPropertyNames.ts, 1, 15), Decl(noRepeatedPropertyNames.ts, 1, 21))

class C {
>C : Symbol(C, Decl(noRepeatedPropertyNames.ts, 1, 29))

m() {
>m : Symbol(C.m, Decl(noRepeatedPropertyNames.ts, 2, 9))

const second = { a: 1, a: 2 };
>second : Symbol(second, Decl(noRepeatedPropertyNames.ts, 4, 13))
>a : Symbol(a, Decl(noRepeatedPropertyNames.ts, 4, 24), Decl(noRepeatedPropertyNames.ts, 4, 30))
>a : Symbol(a, Decl(noRepeatedPropertyNames.ts, 4, 24), Decl(noRepeatedPropertyNames.ts, 4, 30))

return second.a;
>second.a : Symbol(a, Decl(noRepeatedPropertyNames.ts, 4, 24), Decl(noRepeatedPropertyNames.ts, 4, 30))
>second : Symbol(second, Decl(noRepeatedPropertyNames.ts, 4, 13))
>a : Symbol(a, Decl(noRepeatedPropertyNames.ts, 4, 24), Decl(noRepeatedPropertyNames.ts, 4, 30))
}
}

31 changes: 31 additions & 0 deletions tests/baselines/reference/noRepeatedPropertyNames.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
=== tests/cases/compiler/noRepeatedPropertyNames.ts ===
// https://github.com/microsoft/TypeScript/issues/46815
const first = { a: 1, a: 2 };
>first : { a: number; }
>{ a: 1, a: 2 } : { a: number; }
>a : number
>1 : 1
>a : number
>2 : 2

class C {
>C : C

m() {
>m : () => number

const second = { a: 1, a: 2 };
>second : { a: number; }
>{ a: 1, a: 2 } : { a: number; }
>a : number
>1 : 1
>a : number
>2 : 2

return second.a;
>second.a : number
>second : { a: number; }
>a : number
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericString
tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericStringNamedPropertyEquivalence.ts(16,5): error TS2300: Duplicate identifier '1'.
tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericStringNamedPropertyEquivalence.ts(17,5): error TS2300: Duplicate identifier '1'.
tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericStringNamedPropertyEquivalence.ts(17,5): error TS2717: Subsequent property declarations must have the same type. Property '1.0' must be of type 'number', but here has type 'string'.
tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericStringNamedPropertyEquivalence.ts(22,5): error TS2300: Duplicate identifier '0'.
tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericStringNamedPropertyEquivalence.ts(22,5): error TS1117: An object literal cannot have multiple properties with the same name.


==== tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericStringNamedPropertyEquivalence.ts (7 errors) ====
Expand Down Expand Up @@ -44,5 +44,5 @@ tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericString
"0": '',
0: ''
~
!!! error TS2300: Duplicate identifier '0'.
!!! error TS1117: An object literal cannot have multiple properties with the same name.
}
Loading