TypeScript: Bug: Accessing private statics in a class via its derived class is allowed
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)
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 1
- Comments: 22 (10 by maintainers)
Commits related to this issue
- Added error for accessing private statics via derived classes Fixes #8624 — committed to JoshuaKGoldberg/TypeScript by deleted user 5 years ago
@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!
@jnm2 Sorry for making noise, I did not see that you removed your comment before I replied. However, yes I agree, this behavior would be strange considering that TypeScript is copying the static members down the inheritance chain. I was not aware of that, but I think that this copying is even stranger, from a consumer point-of-view. Or who knows, maybe it’s just a feature that I have yet to discover the value of…