Skip to content

Commit 626e78c

Browse files
authored
Clearifies protected error message (#42599)
1 parent 3d53661 commit 626e78c

6 files changed

+62
-63
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26538,7 +26538,7 @@ namespace ts {
2653826538
type = (type as TypeParameter).isThisType ? getConstraintOfTypeParameter(<TypeParameter>type)! : getBaseConstraintOfType(<TypeParameter>type)!; // TODO: GH#18217 Use a different variable that's allowed to be undefined
2653926539
}
2654026540
if (!type || !hasBaseType(type, enclosingClass)) {
26541-
error(errorNode, Diagnostics.Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1, symbolToString(prop), typeToString(enclosingClass));
26541+
error(errorNode, Diagnostics.Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1_This_is_an_instance_of_class_2, symbolToString(prop), typeToString(enclosingClass), typeToString(type));
2654226542
return false;
2654326543
}
2654426544
return true;

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,7 +1962,7 @@
19621962
"category": "Error",
19631963
"code": 2445
19641964
},
1965-
"Property '{0}' is protected and only accessible through an instance of class '{1}'.": {
1965+
"Property '{0}' is protected and only accessible through an instance of class '{1}'. This is an instance of class '{2}'.": {
19661966
"category": "Error",
19671967
"code": 2446
19681968
},
@@ -6258,7 +6258,6 @@
62586258
"code": 95159
62596259
},
62606260

6261-
62626261
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
62636262
"category": "Error",
62646263
"code": 18004

tests/baselines/reference/protectedClassPropertyAccessibleWithinNestedSubclass1.errors.txt

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(15,20): error TS2445: Property 'x' is protected and only accessible within class 'Derived3' and its subclasses.
2-
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(32,19): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived1'.
3-
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(34,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived1'.
2+
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(32,19): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived1'. This is an instance of class 'Base'.
3+
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(34,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived1'. This is an instance of class 'Derived2'.
44
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(35,20): error TS2445: Property 'x' is protected and only accessible within class 'Derived3' and its subclasses.
5-
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(36,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived1'.
6-
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(52,19): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived2'.
7-
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(53,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived2'.
5+
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(36,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived1'. This is an instance of class 'Derived4'.
6+
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(52,19): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived2'. This is an instance of class 'Base'.
7+
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(53,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived2'. This is an instance of class 'Derived1'.
88
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(55,20): error TS2445: Property 'x' is protected and only accessible within class 'Derived3' and its subclasses.
9-
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(73,19): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'.
10-
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(74,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'.
11-
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(75,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'.
12-
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(77,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'.
13-
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(93,19): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived4'.
14-
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(94,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived4'.
15-
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(95,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived4'.
9+
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(73,19): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'. This is an instance of class 'Base'.
10+
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(74,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'. This is an instance of class 'Derived1'.
11+
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(75,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'. This is an instance of class 'Derived2'.
12+
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(77,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'. This is an instance of class 'Derived4'.
13+
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(93,19): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived4'. This is an instance of class 'Base'.
14+
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(94,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived4'. This is an instance of class 'Derived1'.
15+
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(95,20): error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived4'. This is an instance of class 'Derived2'.
1616
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(96,20): error TS2445: Property 'x' is protected and only accessible within class 'Derived3' and its subclasses.
1717
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(110,3): error TS2445: Property 'x' is protected and only accessible within class 'Base' and its subclasses.
1818
tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass1.ts(111,4): error TS2445: Property 'x' is protected and only accessible within class 'Base' and its subclasses.
@@ -57,17 +57,17 @@ tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAcce
5757

5858
b.x; // Error, isn't accessed through an instance of the enclosing class
5959
~
60-
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived1'.
60+
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived1'. This is an instance of class 'Base'.
6161
d1.x; // OK, accessed within a class derived from their declaring class, and through an instance of the enclosing class
6262
d2.x; // Error, isn't accessed through an instance of the enclosing class
6363
~
64-
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived1'.
64+
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived1'. This is an instance of class 'Derived2'.
6565
d3.x; // Error, redefined in a subclass, can only be accessed in the declaring class or one of its subclasses
6666
~
6767
!!! error TS2445: Property 'x' is protected and only accessible within class 'Derived3' and its subclasses.
6868
d4.x; // Error, isn't accessed through an instance of the enclosing class
6969
~
70-
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived1'.
70+
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived1'. This is an instance of class 'Derived4'.
7171
}
7272
}
7373
}
@@ -85,10 +85,10 @@ tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAcce
8585

8686
b.x; // Error, isn't accessed through an instance of the enclosing class
8787
~
88-
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived2'.
88+
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived2'. This is an instance of class 'Base'.
8989
d1.x; // Error, isn't accessed through an instance of the enclosing class
9090
~
91-
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived2'.
91+
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived2'. This is an instance of class 'Derived1'.
9292
d2.x; // OK, accessed within a class derived from their declaring class, and through an instance of the enclosing class
9393
d3.x; // Error, redefined in a subclass, can only be accessed in the declaring class or one of its subclasses
9494
~
@@ -112,17 +112,17 @@ tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAcce
112112

113113
b.x; // Error, isn't accessed through an instance of the enclosing class
114114
~
115-
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'.
115+
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'. This is an instance of class 'Base'.
116116
d1.x; // Error, isn't accessed through an instance of the enclosing class
117117
~
118-
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'.
118+
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'. This is an instance of class 'Derived1'.
119119
d2.x; // Error, isn't accessed through an instance of the enclosing class
120120
~
121-
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'.
121+
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'. This is an instance of class 'Derived2'.
122122
d3.x; // OK, accessed within their declaring class
123123
d4.x; // Error, isn't accessed through an instance of the enclosing class
124124
~
125-
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'.
125+
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived3'. This is an instance of class 'Derived4'.
126126
}
127127
}
128128
}
@@ -140,13 +140,13 @@ tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAcce
140140

141141
b.x; // Error, isn't accessed through an instance of the enclosing class
142142
~
143-
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived4'.
143+
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived4'. This is an instance of class 'Base'.
144144
d1.x; // Error, isn't accessed through an instance of the enclosing class
145145
~
146-
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived4'.
146+
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived4'. This is an instance of class 'Derived1'.
147147
d2.x; // Error, isn't accessed through an instance of the enclosing class
148148
~
149-
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived4'.
149+
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'Derived4'. This is an instance of class 'Derived2'.
150150
d3.x; // Error, redefined in a subclass, can only be accessed in the declaring class or one of its subclasses
151151
~
152152
!!! error TS2445: Property 'x' is protected and only accessible within class 'Derived3' and its subclasses.

0 commit comments

Comments
 (0)