Add a visibility mechanism similar to `friend` or `InternalsVisibleTo`
See original GitHub issueSuggestion
A way to allow certain other modules to access properties or methods of a class.
Use Cases
Sometimes, we want other modules (other code) to have access to certain properties or methods, but otherwise the end user not to have access to the properties/methods. This is labeled as “package protected” or similar terms in other languages.
Examples
This is how it could work, with some sort of new non-runtime syntax:
import SomeClass from './SomeClass'
export class Foo {
visible in SomeClass
private foo: number = 123
}
or
import SomeClass from './SomeClass'
export class Foo {
private foo visible in SomeClass: number = 123
}
or maybe even as a special decorator, globally-declared, virtual decorator
import SomeClass from './SomeClass'
export class Foo {
@visibleIn(SomeClass)
private foo: number = 123
}
SomeClass
would then be able to access the private property/method:
import {Foo} from './Foo'
export class SomeClass {
doSomethingWithFoo(o: Foo) {
console.log(o.foo) // OK, no type error.
}
}
The code inside of SomeClass
could be allowed to access the private properties. This is great at design time when the author controls the source for both sides, but still wants to hide private parts from the end user on the outside of the APIs.
This allows for patterns like object managers being able to control certain aspects of the objects they manage (for example a renderer in a game engine that manages how objects in a scene render, and the renderable properties that the engine manager accesses, which are private to the end user) are composed from reactions to changes in the objects’ public APIs by end users).
At the moment, the only way to allow objects to access properties of other objects is to make them public, but this does not allow for us to hide (at least in the type system) certain implementation details of a cross-class or cross-module whole system from the end user.
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
- I’m not sure about this. This feature wouldn’t change semantics of JavaScript, or add any JavaScript features, but would only add additional type capabilities to limit use of APIs in certain ways. In plain JS, before I ported a JavaScript project to TypeScript, I had an object manager that accessed properties prefixed with
__
to denote that the properties were not to be used by the end user. The engine accessed the properties for internal implementation of the system.
- I’m not sure about this. This feature wouldn’t change semantics of JavaScript, or add any JavaScript features, but would only add additional type capabilities to limit use of APIs in certain ways. In plain JS, before I ported a JavaScript project to TypeScript, I had an object manager that accessed properties prefixed with
Issue Analytics
- State:
- Created 4 years ago
- Reactions:12
- Comments:11
Maybe it could be useful to think of friend visibility at module level instead of classes. Modules are a native encapsulation concept in JavaScript, so it would probably feel more natural to reason on them, especially for the many folks that avoid classes in their programming style.
No need for this madness.