Skip to content

Commit a0e0104

Browse files
authored
error on duplicate symbols in object literals (#55193)
1 parent fe10fc7 commit a0e0104

7 files changed

+493
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49807,7 +49807,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4980749807
function getEffectivePropertyNameForPropertyNameNode(node: PropertyName) {
4980849808
const name = getPropertyNameForPropertyNameNode(node);
4980949809
return name ? name :
49810-
isComputedPropertyName(node) && isEntityNameExpression(node.expression) ? tryGetNameFromEntityNameExpression(node.expression) : undefined;
49810+
isComputedPropertyName(node) ? tryGetNameFromType(getTypeOfExpression(node.expression)) : undefined;
4981149811
}
4981249812

4981349813
function getCombinedModifierFlagsCached(node: Declaration) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
symbolProperty36.ts(3,5): error TS1117: An object literal cannot have multiple properties with the same name.
2+
3+
4+
==== symbolProperty36.ts (1 errors) ====
5+
var x = {
6+
[Symbol.isConcatSpreadable]: 0,
7+
[Symbol.isConcatSpreadable]: 1
8+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
10+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
uniqueSymbolsPropertyNames.ts(27,3): error TS1117: An object literal cannot have multiple properties with the same name.
2+
uniqueSymbolsPropertyNames.ts(28,3): error TS1117: An object literal cannot have multiple properties with the same name.
3+
uniqueSymbolsPropertyNames.ts(33,3): error TS1117: An object literal cannot have multiple properties with the same name.
4+
uniqueSymbolsPropertyNames.ts(38,3): error TS1117: An object literal cannot have multiple properties with the same name.
5+
uniqueSymbolsPropertyNames.ts(43,3): error TS1117: An object literal cannot have multiple properties with the same name.
6+
uniqueSymbolsPropertyNames.ts(45,3): error TS1117: An object literal cannot have multiple properties with the same name.
7+
8+
9+
==== uniqueSymbolsPropertyNames.ts (6 errors) ====
10+
interface OpTypes {
11+
readonly equal: unique symbol;
12+
}
13+
14+
namespace OpNamespace {
15+
export declare const equal: unique symbol;
16+
}
17+
18+
const uniqueSymbol0 = Symbol.for("");
19+
const uniqueSymbol1 = Symbol.for("");
20+
21+
22+
function getUniqueSymbol0(): typeof uniqueSymbol0 {
23+
return uniqueSymbol0;
24+
}
25+
26+
function getUniqueSymbol1(): typeof uniqueSymbol1 {
27+
return uniqueSymbol1;
28+
}
29+
30+
const Op: OpTypes = {
31+
equal: Symbol.for("equal"),
32+
} as OpTypes;
33+
34+
const t0 = {
35+
equal: "first",
36+
equal: "second",
37+
~~~~~
38+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
39+
["equal"]: "last",
40+
~~~~~~~~~
41+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
42+
};
43+
44+
const t1 = {
45+
[Op.equal]: "first",
46+
[Op.equal]: "last",
47+
~~~~~~~~~~
48+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
49+
};
50+
51+
const t2 = {
52+
[OpNamespace.equal]: "first",
53+
[OpNamespace.equal]: "last",
54+
~~~~~~~~~~~~~~~~~~~
55+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
56+
};
57+
58+
const t3 = {
59+
[getUniqueSymbol0()]: "first",
60+
[getUniqueSymbol0()]: "last",
61+
~~~~~~~~~~~~~~~~~~~~
62+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
63+
[getUniqueSymbol1()]: "first",
64+
[getUniqueSymbol1()]: "last",
65+
~~~~~~~~~~~~~~~~~~~~
66+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
67+
};
68+
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//// [tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsPropertyNames.ts] ////
2+
3+
//// [uniqueSymbolsPropertyNames.ts]
4+
interface OpTypes {
5+
readonly equal: unique symbol;
6+
}
7+
8+
namespace OpNamespace {
9+
export declare const equal: unique symbol;
10+
}
11+
12+
const uniqueSymbol0 = Symbol.for("");
13+
const uniqueSymbol1 = Symbol.for("");
14+
15+
16+
function getUniqueSymbol0(): typeof uniqueSymbol0 {
17+
return uniqueSymbol0;
18+
}
19+
20+
function getUniqueSymbol1(): typeof uniqueSymbol1 {
21+
return uniqueSymbol1;
22+
}
23+
24+
const Op: OpTypes = {
25+
equal: Symbol.for("equal"),
26+
} as OpTypes;
27+
28+
const t0 = {
29+
equal: "first",
30+
equal: "second",
31+
["equal"]: "last",
32+
};
33+
34+
const t1 = {
35+
[Op.equal]: "first",
36+
[Op.equal]: "last",
37+
};
38+
39+
const t2 = {
40+
[OpNamespace.equal]: "first",
41+
[OpNamespace.equal]: "last",
42+
};
43+
44+
const t3 = {
45+
[getUniqueSymbol0()]: "first",
46+
[getUniqueSymbol0()]: "last",
47+
[getUniqueSymbol1()]: "first",
48+
[getUniqueSymbol1()]: "last",
49+
};
50+
51+
52+
//// [uniqueSymbolsPropertyNames.js]
53+
var OpNamespace;
54+
(function (OpNamespace) {
55+
})(OpNamespace || (OpNamespace = {}));
56+
const uniqueSymbol0 = Symbol.for("");
57+
const uniqueSymbol1 = Symbol.for("");
58+
function getUniqueSymbol0() {
59+
return uniqueSymbol0;
60+
}
61+
function getUniqueSymbol1() {
62+
return uniqueSymbol1;
63+
}
64+
const Op = {
65+
equal: Symbol.for("equal"),
66+
};
67+
const t0 = {
68+
equal: "first",
69+
equal: "second",
70+
["equal"]: "last",
71+
};
72+
const t1 = {
73+
[Op.equal]: "first",
74+
[Op.equal]: "last",
75+
};
76+
const t2 = {
77+
[OpNamespace.equal]: "first",
78+
[OpNamespace.equal]: "last",
79+
};
80+
const t3 = {
81+
[getUniqueSymbol0()]: "first",
82+
[getUniqueSymbol0()]: "last",
83+
[getUniqueSymbol1()]: "first",
84+
[getUniqueSymbol1()]: "last",
85+
};
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
//// [tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsPropertyNames.ts] ////
2+
3+
=== uniqueSymbolsPropertyNames.ts ===
4+
interface OpTypes {
5+
>OpTypes : Symbol(OpTypes, Decl(uniqueSymbolsPropertyNames.ts, 0, 0))
6+
7+
readonly equal: unique symbol;
8+
>equal : Symbol(OpTypes.equal, Decl(uniqueSymbolsPropertyNames.ts, 0, 19))
9+
}
10+
11+
namespace OpNamespace {
12+
>OpNamespace : Symbol(OpNamespace, Decl(uniqueSymbolsPropertyNames.ts, 2, 1))
13+
14+
export declare const equal: unique symbol;
15+
>equal : Symbol(equal, Decl(uniqueSymbolsPropertyNames.ts, 5, 22))
16+
}
17+
18+
const uniqueSymbol0 = Symbol.for("");
19+
>uniqueSymbol0 : Symbol(uniqueSymbol0, Decl(uniqueSymbolsPropertyNames.ts, 8, 5))
20+
>Symbol.for : Symbol(SymbolConstructor.for, Decl(lib.es2015.symbol.d.ts, --, --))
21+
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
22+
>for : Symbol(SymbolConstructor.for, Decl(lib.es2015.symbol.d.ts, --, --))
23+
24+
const uniqueSymbol1 = Symbol.for("");
25+
>uniqueSymbol1 : Symbol(uniqueSymbol1, Decl(uniqueSymbolsPropertyNames.ts, 9, 5))
26+
>Symbol.for : Symbol(SymbolConstructor.for, Decl(lib.es2015.symbol.d.ts, --, --))
27+
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
28+
>for : Symbol(SymbolConstructor.for, Decl(lib.es2015.symbol.d.ts, --, --))
29+
30+
31+
function getUniqueSymbol0(): typeof uniqueSymbol0 {
32+
>getUniqueSymbol0 : Symbol(getUniqueSymbol0, Decl(uniqueSymbolsPropertyNames.ts, 9, 37))
33+
>uniqueSymbol0 : Symbol(uniqueSymbol0, Decl(uniqueSymbolsPropertyNames.ts, 8, 5))
34+
35+
return uniqueSymbol0;
36+
>uniqueSymbol0 : Symbol(uniqueSymbol0, Decl(uniqueSymbolsPropertyNames.ts, 8, 5))
37+
}
38+
39+
function getUniqueSymbol1(): typeof uniqueSymbol1 {
40+
>getUniqueSymbol1 : Symbol(getUniqueSymbol1, Decl(uniqueSymbolsPropertyNames.ts, 14, 1))
41+
>uniqueSymbol1 : Symbol(uniqueSymbol1, Decl(uniqueSymbolsPropertyNames.ts, 9, 5))
42+
43+
return uniqueSymbol1;
44+
>uniqueSymbol1 : Symbol(uniqueSymbol1, Decl(uniqueSymbolsPropertyNames.ts, 9, 5))
45+
}
46+
47+
const Op: OpTypes = {
48+
>Op : Symbol(Op, Decl(uniqueSymbolsPropertyNames.ts, 20, 5))
49+
>OpTypes : Symbol(OpTypes, Decl(uniqueSymbolsPropertyNames.ts, 0, 0))
50+
51+
equal: Symbol.for("equal"),
52+
>equal : Symbol(equal, Decl(uniqueSymbolsPropertyNames.ts, 20, 21))
53+
>Symbol.for : Symbol(SymbolConstructor.for, Decl(lib.es2015.symbol.d.ts, --, --))
54+
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
55+
>for : Symbol(SymbolConstructor.for, Decl(lib.es2015.symbol.d.ts, --, --))
56+
57+
} as OpTypes;
58+
>OpTypes : Symbol(OpTypes, Decl(uniqueSymbolsPropertyNames.ts, 0, 0))
59+
60+
const t0 = {
61+
>t0 : Symbol(t0, Decl(uniqueSymbolsPropertyNames.ts, 24, 5))
62+
63+
equal: "first",
64+
>equal : Symbol(equal, Decl(uniqueSymbolsPropertyNames.ts, 24, 12), Decl(uniqueSymbolsPropertyNames.ts, 25, 17), Decl(uniqueSymbolsPropertyNames.ts, 26, 18))
65+
66+
equal: "second",
67+
>equal : Symbol(equal, Decl(uniqueSymbolsPropertyNames.ts, 24, 12), Decl(uniqueSymbolsPropertyNames.ts, 25, 17), Decl(uniqueSymbolsPropertyNames.ts, 26, 18))
68+
69+
["equal"]: "last",
70+
>["equal"] : Symbol(equal, Decl(uniqueSymbolsPropertyNames.ts, 24, 12), Decl(uniqueSymbolsPropertyNames.ts, 25, 17), Decl(uniqueSymbolsPropertyNames.ts, 26, 18))
71+
>"equal" : Symbol(equal, Decl(uniqueSymbolsPropertyNames.ts, 24, 12), Decl(uniqueSymbolsPropertyNames.ts, 25, 17), Decl(uniqueSymbolsPropertyNames.ts, 26, 18))
72+
73+
};
74+
75+
const t1 = {
76+
>t1 : Symbol(t1, Decl(uniqueSymbolsPropertyNames.ts, 30, 5))
77+
78+
[Op.equal]: "first",
79+
>[Op.equal] : Symbol([Op.equal], Decl(uniqueSymbolsPropertyNames.ts, 30, 12), Decl(uniqueSymbolsPropertyNames.ts, 31, 22))
80+
>Op.equal : Symbol(OpTypes.equal, Decl(uniqueSymbolsPropertyNames.ts, 0, 19))
81+
>Op : Symbol(Op, Decl(uniqueSymbolsPropertyNames.ts, 20, 5))
82+
>equal : Symbol(OpTypes.equal, Decl(uniqueSymbolsPropertyNames.ts, 0, 19))
83+
84+
[Op.equal]: "last",
85+
>[Op.equal] : Symbol([Op.equal], Decl(uniqueSymbolsPropertyNames.ts, 30, 12), Decl(uniqueSymbolsPropertyNames.ts, 31, 22))
86+
>Op.equal : Symbol(OpTypes.equal, Decl(uniqueSymbolsPropertyNames.ts, 0, 19))
87+
>Op : Symbol(Op, Decl(uniqueSymbolsPropertyNames.ts, 20, 5))
88+
>equal : Symbol(OpTypes.equal, Decl(uniqueSymbolsPropertyNames.ts, 0, 19))
89+
90+
};
91+
92+
const t2 = {
93+
>t2 : Symbol(t2, Decl(uniqueSymbolsPropertyNames.ts, 35, 5))
94+
95+
[OpNamespace.equal]: "first",
96+
>[OpNamespace.equal] : Symbol([OpNamespace.equal], Decl(uniqueSymbolsPropertyNames.ts, 35, 12), Decl(uniqueSymbolsPropertyNames.ts, 36, 31))
97+
>OpNamespace.equal : Symbol(OpNamespace.equal, Decl(uniqueSymbolsPropertyNames.ts, 5, 22))
98+
>OpNamespace : Symbol(OpNamespace, Decl(uniqueSymbolsPropertyNames.ts, 2, 1))
99+
>equal : Symbol(OpNamespace.equal, Decl(uniqueSymbolsPropertyNames.ts, 5, 22))
100+
101+
[OpNamespace.equal]: "last",
102+
>[OpNamespace.equal] : Symbol([OpNamespace.equal], Decl(uniqueSymbolsPropertyNames.ts, 35, 12), Decl(uniqueSymbolsPropertyNames.ts, 36, 31))
103+
>OpNamespace.equal : Symbol(OpNamespace.equal, Decl(uniqueSymbolsPropertyNames.ts, 5, 22))
104+
>OpNamespace : Symbol(OpNamespace, Decl(uniqueSymbolsPropertyNames.ts, 2, 1))
105+
>equal : Symbol(OpNamespace.equal, Decl(uniqueSymbolsPropertyNames.ts, 5, 22))
106+
107+
};
108+
109+
const t3 = {
110+
>t3 : Symbol(t3, Decl(uniqueSymbolsPropertyNames.ts, 40, 5))
111+
112+
[getUniqueSymbol0()]: "first",
113+
>[getUniqueSymbol0()] : Symbol([getUniqueSymbol0()], Decl(uniqueSymbolsPropertyNames.ts, 40, 12))
114+
>getUniqueSymbol0 : Symbol(getUniqueSymbol0, Decl(uniqueSymbolsPropertyNames.ts, 9, 37))
115+
116+
[getUniqueSymbol0()]: "last",
117+
>[getUniqueSymbol0()] : Symbol([getUniqueSymbol0()], Decl(uniqueSymbolsPropertyNames.ts, 41, 32))
118+
>getUniqueSymbol0 : Symbol(getUniqueSymbol0, Decl(uniqueSymbolsPropertyNames.ts, 9, 37))
119+
120+
[getUniqueSymbol1()]: "first",
121+
>[getUniqueSymbol1()] : Symbol([getUniqueSymbol1()], Decl(uniqueSymbolsPropertyNames.ts, 42, 31))
122+
>getUniqueSymbol1 : Symbol(getUniqueSymbol1, Decl(uniqueSymbolsPropertyNames.ts, 14, 1))
123+
124+
[getUniqueSymbol1()]: "last",
125+
>[getUniqueSymbol1()] : Symbol([getUniqueSymbol1()], Decl(uniqueSymbolsPropertyNames.ts, 43, 32))
126+
>getUniqueSymbol1 : Symbol(getUniqueSymbol1, Decl(uniqueSymbolsPropertyNames.ts, 14, 1))
127+
128+
};
129+

0 commit comments

Comments
 (0)