-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow protected members to be accessed within the enclosing class #60001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Allow protected members to be accessed within the enclosing class #60001
Conversation
@@ -33868,7 +33868,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |||
// get the original type -- represented as the type constraint of the 'this' type | |||
containingType = (containingType as TypeParameter).isThisType ? getConstraintOfTypeParameter(containingType as TypeParameter)! : getBaseConstraintOfType(containingType as TypeParameter)!; // TODO: GH#18217 Use a different variable that's allowed to be undefined | |||
} | |||
if (!containingType || !hasBaseType(containingType, enclosingClass)) { | |||
if (!containingType || !hasBaseType(containingType, enclosingClass) && !isNodeWithinClass(location, getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)!)!)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the added check is the same one as for private properties a couple of lines above this (in this function)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does besides the null checking; is it really safe to !
all of these? The other case doesn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other case does exactly the same. I copy-pasted this :p
// Private property is accessible if the property is within the declaring class
if (flags & ModifierFlags.Private) {
const declaringClassDeclaration = getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)!)!;
if (!isNodeWithinClass(location, declaringClassDeclaration)) {
if (errorNode) {
error(errorNode, Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(getDeclaringClass(prop)!));
}
return false;
}
return true;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yeah, I can't read
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we just do this check unconditionally at the top? It sure seems like the result of this code is that "you can access any property of the current class as long as you're within that class", such that we don't need to do all of this runaround.
71bbed7
to
f00d00b
Compare
@typescript-bot test it |
In a way this feels wrong; I remember on my first checker bugfix that we didn't want to let one spread out private props which feels similar: #47078 (comment) But, allowing private and not protected seems the oddest of all. |
Hey @jakebailey, the results of running the DT tests are ready. Everything looks the same! |
Honestly, I don't really know what are the expectations around private/protected when it comes to closures etc. Private properties are allowed in those positions quite explicitly by the code (and IIRC there are tests for it) so this merely matches the behavior of protected properties with that. I don't feel anything in the linked comment says strongly this might be wrong:
Those references to this class here are within the class ;p I feel like the linked comment is mainly about object types created by spreads than about other cases like this here. |
@jakebailey Here are the results of running the user tests with tsc comparing Everything looks good! |
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
@jakebailey Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
fixes #59989