Skip to content

Commit 2169928

Browse files
committed
Protected constructors now accessible everywhere in subclasses
1 parent 97ef839 commit 2169928

File tree

4 files changed

+7
-13
lines changed

4 files changed

+7
-13
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11544,18 +11544,15 @@ namespace ts {
1154411544
const declaringClassDeclaration = <ClassLikeDeclaration>getClassLikeDeclarationOfSymbol(declaration.parent.symbol);
1154511545
const declaringClass = <InterfaceType>getDeclaredTypeOfSymbol(declaration.parent.symbol);
1154611546

11547-
// A private or protected constructor can only be instantiated within its own class or a static method of a subclass
11547+
// A private or protected constructor can only be instantiated within its own class (or a subclass, for protected)
1154811548
if (!isNodeWithinClass(node, declaringClassDeclaration)) {
11549-
const containingFunction = getContainingFunction(node);
1155011549
const containingClass = getContainingClass(node);
1155111550
if (containingClass) {
1155211551
const containingType = getTypeOfNode(containingClass);
1155311552
const baseTypes = getBaseTypes(<InterfaceType>containingType);
1155411553
if (baseTypes.length) {
1155511554
const baseType = baseTypes[0];
11556-
if (containingFunction &&
11557-
containingFunction.flags & NodeFlags.Static &&
11558-
flags & NodeFlags.Protected &&
11555+
if (flags & NodeFlags.Protected &&
1155911556
baseType.symbol === declaration.parent.symbol) {
1156011557
return true;
1156111558
}

tests/baselines/reference/classConstructorAccessibility2.errors.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(28,28): error TS2674: Constructor of class 'BaseB' is protected and only accessible within the class declaration.
21
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(32,24): error TS2675: Cannot extend a class 'BaseC'. Class constructor is marked as private.
32
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(35,28): error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration.
43
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(36,35): error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration.
54
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(40,10): error TS2674: Constructor of class 'BaseB' is protected and only accessible within the class declaration.
65
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(41,10): error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration.
76

87

9-
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts (6 errors) ====
8+
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts (5 errors) ====
109

1110
class BaseA {
1211
public constructor(public x: number) { }
@@ -34,9 +33,7 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib
3433
class DerivedB extends BaseB {
3534
constructor(public x: number) { super(x); }
3635
createInstance() { new DerivedB(7); }
37-
createBaseInstance() { new BaseB(8); } // error
38-
~~~~~~~~~~~~
39-
!!! error TS2674: Constructor of class 'BaseB' is protected and only accessible within the class declaration.
36+
createBaseInstance() { new BaseB(8); } // ok
4037
static staticBaseInstance() { new BaseB(9); } // ok
4138
}
4239

tests/baselines/reference/classConstructorAccessibility2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class DerivedA extends BaseA {
2626
class DerivedB extends BaseB {
2727
constructor(public x: number) { super(x); }
2828
createInstance() { new DerivedB(7); }
29-
createBaseInstance() { new BaseB(8); } // error
29+
createBaseInstance() { new BaseB(8); } // ok
3030
static staticBaseInstance() { new BaseB(9); } // ok
3131
}
3232

@@ -92,7 +92,7 @@ var DerivedB = (function (_super) {
9292
this.x = x;
9393
}
9494
DerivedB.prototype.createInstance = function () { new DerivedB(7); };
95-
DerivedB.prototype.createBaseInstance = function () { new BaseB(8); }; // error
95+
DerivedB.prototype.createBaseInstance = function () { new BaseB(8); }; // ok
9696
DerivedB.staticBaseInstance = function () { new BaseB(9); }; // ok
9797
return DerivedB;
9898
}(BaseB));

tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class DerivedA extends BaseA {
2626
class DerivedB extends BaseB {
2727
constructor(public x: number) { super(x); }
2828
createInstance() { new DerivedB(7); }
29-
createBaseInstance() { new BaseB(8); } // error
29+
createBaseInstance() { new BaseB(8); } // ok
3030
static staticBaseInstance() { new BaseB(9); } // ok
3131
}
3232

0 commit comments

Comments
 (0)