From 97ef839a03fc16ea1aa5a7242ed68ad26814443f Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Fri, 22 Jul 2016 13:48:43 -0700 Subject: [PATCH] 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. --- src/compiler/checker.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1cebd469388..ff248d75941 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11544,8 +11544,23 @@ namespace ts { const declaringClassDeclaration = getClassLikeDeclarationOfSymbol(declaration.parent.symbol); const declaringClass = getDeclaredTypeOfSymbol(declaration.parent.symbol); - // A private or protected constructor can only be instantiated within it's own class + // A private or protected constructor can only be instantiated within its own class or a static method of a subclass if (!isNodeWithinClass(node, declaringClassDeclaration)) { + const containingFunction = getContainingFunction(node); + const containingClass = getContainingClass(node); + if (containingClass) { + const containingType = getTypeOfNode(containingClass); + const baseTypes = getBaseTypes(containingType); + if (baseTypes.length) { + const baseType = baseTypes[0]; + if (containingFunction && + containingFunction.flags & NodeFlags.Static && + flags & NodeFlags.Protected && + baseType.symbol === declaration.parent.symbol) { + return true; + } + } + } if (flags & NodeFlags.Private) { error(node, Diagnostics.Constructor_of_class_0_is_private_and_only_accessible_within_the_class_declaration, typeToString(declaringClass)); }