Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
sandersn committed Jul 22, 2016
commit 97ef839a03fc16ea1aa5a7242ed68ad26814443f
17 changes: 16 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11544,8 +11544,23 @@ 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 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(<InterfaceType>containingType);
if (baseTypes.length) {
const baseType = baseTypes[0];
if (containingFunction &&
containingFunction.flags & NodeFlags.Static &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it matter whether the function is static?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. I wasn't paying attention and overfit to the example. I see that instance methods should be ok based on your constructor-with-super equivalence example.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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));
}
Expand Down