microsoft / microsoft/TypeScript
Add a visibility mechanism similar to `friend` or `InternalsVisibleTo`
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Suggestion
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing TypeScript's existing private-member visibility rules and the design goals linked in the issue. The proposal does not identify implementation files, tests, or a settled syntax; work would first require defining the syntax, module-access semantics, and type-checking behavior before implementation and tests could establish completion.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100