question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Accessing private statics in a class via its derived class is allowed

See original GitHub issue

TypeScript Version:

1.8.X

Code

class FooBase {
    private static privateStatic: string = "";

    testBase(): void {
        console.log(FooBase.privateStatic);

        // Should error, but doesn't
        console.log(Foo.privateStatic);
   }
}

class Foo extends FooBase {
    test() {
        // Should error, and does
        console.log(Foo.privateStatic);
    }
}

Expected behavior: Accessing Foo.privateStatic shouldn’t be allowed, regardless of the context it’s called in.

Actual behavior: FooBase is allowed to do this.

(thanks @sethbrenith for deducing this)

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:22 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
Elephant-Vesselcommented, Sep 28, 2016

@RyanCavanaugh

Agree that you should not be able to access derived class private statics through their name

If we eventually get some breaking change regarding privates here, maybe we could do two birds at once and remove the general access to privates in a T from within any T, https://github.com/Microsoft/TypeScript/issues/471, giving us better encapsulation in the language?

1reaction
daladaysoftcommented, Sep 11, 2016

Just my 2 cents. Static members fail to integrate well with inheritance. It makes no sense to have them heritable. Static members are services attached to the Class definition and have nothing to do with instances of Class or inherited Class. ActionScript (ES4) works like this, and it is useful and clear. Another (and better) useful approach is Java. With Typescript, I got errors when using statics as if there where not inherited (same names in parent and sub classes, different signatures). In fact, the compiler produce an error when I override a method with another signature than defined in parent class, whatever the method is static or instanced. But generated Javascript works very well. May be it is too late to change this, and some guys could depend now on actual behavior. Or may be, for another reasons I do not figure. Whatever, Typescript is well featured and does a good job. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can derived class access private static member function
No, f should not be accessible via Derived (except a member function) as Base is inherited privately. GCC correctly reports this error:
Read more >
Class can't access its own private static constexpr method ...
Coding example for the question Class can't access its own private static constexpr method - Clang bug?-C++.
Read more >
Using Properties - C# Programming Guide
A property overriding a virtual property can also be sealed, specifying that for derived classes it's no longer virtual. Lastly, a property can ......
Read more >
Inheritance - Learning the Java Language
A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested...
Read more >
Access Modifiers in C++
The output of the above program is a compile time error because we are not allowed to access the private data members of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found