Skip to content

Commit c68d1dd

Browse files
authored
error on duplicate symbols in classes (#55438)
1 parent 0f91f7d commit c68d1dd

22 files changed

+4165
-310
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12725,7 +12725,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1272512725
// or if we have another early-bound symbol declaration with the same name and
1272612726
// conflicting flags.
1272712727
const earlySymbol = earlySymbols && earlySymbols.get(memberName);
12728-
if (lateSymbol.flags & getExcludedSymbolFlags(symbolFlags) || earlySymbol) {
12728+
// Duplicate property declarations of classes are checked in checkClassForDuplicateDeclarations.
12729+
if (!(parent.flags & SymbolFlags.Class) && (lateSymbol.flags & getExcludedSymbolFlags(symbolFlags) || earlySymbol)) {
1272912730
// If we have an existing early-bound member, combine its declarations so that we can
1273012731
// report an error at each declaration.
1273112732
const declarations = earlySymbol ? concatenate(earlySymbol.declarations, lateSymbol.declarations) : lateSymbol.declarations;
@@ -38963,7 +38964,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3896338964
isStaticMember ? staticNames :
3896438965
instanceNames;
3896538966

38966-
const memberName = name && getPropertyNameForPropertyNameNode(name);
38967+
const memberName = name && getEffectivePropertyNameForPropertyNameNode(name);
3896738968
if (memberName) {
3896838969
switch (member.kind) {
3896938970
case SyntaxKind.GetAccessor:
@@ -39032,7 +39033,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3903239033
const memberNameNode = member.name;
3903339034
const isStaticMember = isStatic(member);
3903439035
if (isStaticMember && memberNameNode) {
39035-
const memberName = getPropertyNameForPropertyNameNode(memberNameNode);
39036+
const memberName = getEffectivePropertyNameForPropertyNameNode(memberNameNode);
3903639037
switch (memberName) {
3903739038
case "name":
3903839039
case "length":

tests/baselines/reference/mappedTypeProperties.errors.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ mappedTypeProperties.ts(23,5): error TS7061: A mapped type may not declare prope
66
mappedTypeProperties.ts(27,5): error TS7061: A mapped type may not declare properties or methods.
77
mappedTypeProperties.ts(31,5): error TS7061: A mapped type may not declare properties or methods.
88
mappedTypeProperties.ts(34,5): error TS7061: A mapped type may not declare properties or methods.
9+
mappedTypeProperties.ts(34,6): error TS2304: Cannot find name 'P'.
10+
mappedTypeProperties.ts(34,11): error TS2693: 'PlaceType' only refers to a type, but is being used as a value here.
911
mappedTypeProperties.ts(37,5): error TS7061: A mapped type may not declare properties or methods.
12+
mappedTypeProperties.ts(37,6): error TS2304: Cannot find name 'P'.
13+
mappedTypeProperties.ts(37,11): error TS2693: 'PlaceType' only refers to a type, but is being used as a value here.
1014
mappedTypeProperties.ts(40,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
1115
mappedTypeProperties.ts(40,6): error TS2304: Cannot find name 'P'.
1216
mappedTypeProperties.ts(40,6): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
1317
mappedTypeProperties.ts(40,11): error TS2322: Type 'string' is not assignable to type 'object'.
1418
mappedTypeProperties.ts(40,17): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
1519

1620

17-
==== mappedTypeProperties.ts (14 errors) ====
21+
==== mappedTypeProperties.ts (18 errors) ====
1822
export type PlaceType = 'openSky' | 'roofed' | 'garage'
1923
type Before = {
2024
model: 'hour' | 'day';
@@ -65,11 +69,19 @@ mappedTypeProperties.ts(40,17): error TS2363: The right-hand side of an arithmet
6569
[P in PlaceType]: any
6670
~~~~~~~~~~~~~~~~
6771
!!! error TS7061: A mapped type may not declare properties or methods.
72+
~
73+
!!! error TS2304: Cannot find name 'P'.
74+
~~~~~~~~~
75+
!!! error TS2693: 'PlaceType' only refers to a type, but is being used as a value here.
6876
}
6977
const D = class {
7078
[P in PlaceType]: any
7179
~~~~~~~~~~~~~~~~
7280
!!! error TS7061: A mapped type may not declare properties or methods.
81+
~
82+
!!! error TS2304: Cannot find name 'P'.
83+
~~~~~~~~~
84+
!!! error TS2693: 'PlaceType' only refers to a type, but is being used as a value here.
7385
}
7486
const E = class {
7587
[P in 'a' | 'b']: any

0 commit comments

Comments
 (0)