Skip to content

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

Closed

Description

@0815fox

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)

Activity

  1. RyanCavanaugh commented on Oct 25, 2016

    @RyanCavanaugh
    Member

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

  2. 0815fox commented on Oct 25, 2016

    @0815fox
    Author

    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.

  3. added
    DuplicateAn existing issue was already created
    and removed
    Needs More InfoThe issue still hasn't been fully clarified
    DuplicateAn existing issue was already created
    on Oct 25, 2016
  4. RyanCavanaugh commented on Oct 25, 2016

    @RyanCavanaugh
    Member

    Please see the linked issues.

  5. 0815fox commented on Oct 25, 2016

    @0815fox
    Author

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

  6. aluanhaddad commented on Oct 25, 2016

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

  7. aluanhaddad commented on Oct 25, 2016

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

  8. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions