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.

Add a visibility mechanism similar to `friend` or `InternalsVisibleTo`

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:12
  • Comments:11

github_iconTop GitHub Comments

8reactions
lorenzodallavecchiacommented, Dec 11, 2019

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.

6reactions
hinellcommented, Jan 23, 2022

No need for this madness.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Friend assemblies (or how to make internals visible to other ...
To denote the friend assemblies we add the InternalsVisibleTo attribute to the assembly who hosts the internals and specify the friend ...
Read more >
InternalsVisibleTo with "private protected" - Stack Overflow
Yes, classes in you friendly test-assembly that derives from your base-class will get access to private protected members.
Read more >
InternalsVisibleTo multiple assemblies, Internals visible to .net Core ...
InternalsVisibleTo attribute in the attributes passed to CreateFSharpAssemblyInfo the AssemblyInfo.fs file becomes un-compilable because it will add -1 to ...
Read more >
Problem with InternalsVisibleTo attibute for Friend assemblies
I want to use this to make a strong-named assembly's internals visible to my unit test assembly, but I am getting this error...
Read more >
Posts Tagged With Mocking - Musing, Rants & Jumbled ...
NET programmers. To help focus our tests to only the code we want to validate, Mocking Frameworks are a powerful tool in our...
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