You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: Accessing protected members from a static function in a derived class is declined. #11832
classC1{protectedfoo(){};staticgenerateFor(x:C1){constRetVal=newC1();RetVal.foo();//Compiler allows access of protected member from static method in the class itselfreturnRetVal;}}classC2extendsC1{staticgenerateFor(x:C2){constRetVal=newC1();RetVal.foo();//Compiler disallows access of protected member from static method in child class//error TS2446: Property 'foo' is protected and only accessible through an instance of class 'C2'.returnRetVal;}}
Expected behavior: A static function in a class extending a class C can access all protected and public, but not private members of instances of C
Actual behavior: Protected members are not accessible,Compiler throws TS2446
For reference the simple case without inheritance, which works as expected:
A static function in a class C can access all private,protected and public members of instances of C (works as expected)
Why would a static method be allowed to do something an instance method can't? See also #11278, #10637, #8512
Instance methods can access protected members of the base class. That's the purpose of protected.
Just thinking out loud here but if ECMAScript ever introduces nested, instance member classes, we could end up with something interesting like Scala's path-dependent types.
Actually I suppose we already have that with class expressions. As much as I think classes are over used there is something really interesting about combining classes and closures...
Activity
RyanCavanaugh commentedon Oct 25, 2016
Why would a static method be allowed to do something an instance method can't? See also #11278, #10637, #8512
0815fox commentedon Oct 25, 2016
RyanCavanaugh commentedon Oct 25, 2016
Please see the linked issues.
0815fox commentedon Oct 25, 2016
Okay, I see the point in #8512. I will discuss on there.
aluanhaddad commentedon Oct 25, 2016
Just thinking out loud here but if ECMAScript ever introduces nested, instance member classes, we could end up with something interesting like Scala's path-dependent types.
aluanhaddad commentedon Oct 25, 2016
Actually I suppose we already have that with class expressions. As much as I think classes are over used there is something really interesting about combining classes and closures...