Protected constructors now accessible everywhere in subclasses

This commit is contained in:
Nathan Shively-Sanders
2016-07-22 17:38:25 -07:00
parent 97ef839a03
commit 2169928f2b
4 changed files with 7 additions and 13 deletions

View File

@@ -11544,18 +11544,15 @@ namespace ts {
const declaringClassDeclaration = <ClassLikeDeclaration>getClassLikeDeclarationOfSymbol(declaration.parent.symbol);
const declaringClass = <InterfaceType>getDeclaredTypeOfSymbol(declaration.parent.symbol);
// A private or protected constructor can only be instantiated within its own class or a static method of a subclass
// A private or protected constructor can only be instantiated within its own class (or a subclass, for protected)
if (!isNodeWithinClass(node, declaringClassDeclaration)) {
const containingFunction = getContainingFunction(node);
const containingClass = getContainingClass(node);
if (containingClass) {
const containingType = getTypeOfNode(containingClass);
const baseTypes = getBaseTypes(<InterfaceType>containingType);
if (baseTypes.length) {
const baseType = baseTypes[0];
if (containingFunction &&
containingFunction.flags & NodeFlags.Static &&
flags & NodeFlags.Protected &&
if (flags & NodeFlags.Protected &&
baseType.symbol === declaration.parent.symbol) {
return true;
}

View File

@@ -1,4 +1,3 @@
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(28,28): error TS2674: Constructor of class 'BaseB' is protected and only accessible within the class declaration.
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(32,24): error TS2675: Cannot extend a class 'BaseC'. Class constructor is marked as private.
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(35,28): error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration.
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(36,35): error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration.
@@ -6,7 +5,7 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(41,10): error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration.
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts (6 errors) ====
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts (5 errors) ====
class BaseA {
public constructor(public x: number) { }
@@ -34,9 +33,7 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib
class DerivedB extends BaseB {
constructor(public x: number) { super(x); }
createInstance() { new DerivedB(7); }
createBaseInstance() { new BaseB(8); } // error
~~~~~~~~~~~~
!!! error TS2674: Constructor of class 'BaseB' is protected and only accessible within the class declaration.
createBaseInstance() { new BaseB(8); } // ok
static staticBaseInstance() { new BaseB(9); } // ok
}

View File

@@ -26,7 +26,7 @@ class DerivedA extends BaseA {
class DerivedB extends BaseB {
constructor(public x: number) { super(x); }
createInstance() { new DerivedB(7); }
createBaseInstance() { new BaseB(8); } // error
createBaseInstance() { new BaseB(8); } // ok
static staticBaseInstance() { new BaseB(9); } // ok
}
@@ -92,7 +92,7 @@ var DerivedB = (function (_super) {
this.x = x;
}
DerivedB.prototype.createInstance = function () { new DerivedB(7); };
DerivedB.prototype.createBaseInstance = function () { new BaseB(8); }; // error
DerivedB.prototype.createBaseInstance = function () { new BaseB(8); }; // ok
DerivedB.staticBaseInstance = function () { new BaseB(9); }; // ok
return DerivedB;
}(BaseB));

View File

@@ -26,7 +26,7 @@ class DerivedA extends BaseA {
class DerivedB extends BaseB {
constructor(public x: number) { super(x); }
createInstance() { new DerivedB(7); }
createBaseInstance() { new BaseB(8); } // error
createBaseInstance() { new BaseB(8); } // ok
static staticBaseInstance() { new BaseB(9); } // ok
}