Skip to content

Bug: Accessing protected members from a static function in a derived class is declined. #11832

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

Closed
0815fox opened this issue Oct 25, 2016 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@0815fox
Copy link

0815fox commented Oct 25, 2016

TypeScript Version: 2.0.3

class C1 {
    protected foo() {};
    static generateFor(x:C1) {
        const RetVal = new C1();
        RetVal.foo(); //Compiler allows access of protected member from static method in the class itself
        return RetVal;
    }
}

class C2 extends C1 {
    static generateFor(x:C2) {
        const RetVal = new C1();
        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'.
        return RetVal;
    }
}

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)
@RyanCavanaugh
Copy link
Member

Why would a static method be allowed to do something an instance method can't? See also #11278, #10637, #8512

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Oct 25, 2016
@0815fox
Copy link
Author

0815fox commented Oct 25, 2016

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.

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Needs More Info The issue still hasn't been fully clarified Duplicate An existing issue was already created labels Oct 25, 2016
@RyanCavanaugh
Copy link
Member

Please see the linked issues.

@0815fox
Copy link
Author

0815fox commented Oct 25, 2016

Okay, I see the point in #8512. I will discuss on there.

@aluanhaddad
Copy link
Contributor

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
Copy link
Contributor

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...

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants