Bug: Accessing private statics in a class via its derived class is allowed
See original GitHub issueTypeScript 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:
- Created 7 years ago
- Reactions:1
- Comments:22 (10 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@RyanCavanaugh
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?
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!