Skip to content

Commit 97ef839

Browse files
committed
Protected ctors are accessible in subclass static methods
Previously, it was an error to refer to a protected constructor from a base class, even in a static method where the semantics work. Now it is not an error in static methods.
1 parent 2552560 commit 97ef839

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11544,8 +11544,23 @@ 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 it's own class
11547+
// A private or protected constructor can only be instantiated within its own class or a static method of a subclass
1154811548
if (!isNodeWithinClass(node, declaringClassDeclaration)) {
11549+
const containingFunction = getContainingFunction(node);
11550+
const containingClass = getContainingClass(node);
11551+
if (containingClass) {
11552+
const containingType = getTypeOfNode(containingClass);
11553+
const baseTypes = getBaseTypes(<InterfaceType>containingType);
11554+
if (baseTypes.length) {
11555+
const baseType = baseTypes[0];
11556+
if (containingFunction &&
11557+
containingFunction.flags & NodeFlags.Static &&
11558+
flags & NodeFlags.Protected &&
11559+
baseType.symbol === declaration.parent.symbol) {
11560+
return true;
11561+
}
11562+
}
11563+
}
1154911564
if (flags & NodeFlags.Private) {
1155011565
error(node, Diagnostics.Constructor_of_class_0_is_private_and_only_accessible_within_the_class_declaration, typeToString(declaringClass));
1155111566
}

0 commit comments

Comments
 (0)