-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Friend Declarations for classes #7692
Comments
duplicate of #2136 |
#5228 is the target dupe of that one, but I assume Ron knows the difference between the two |
The desire of Their workaround: class Layout {
private template: Template;
}
function compile(layout: Layout, environment: Environment): CompiledBlock {
return layout['template'].compile(environment);
} This proposal: class Layout {
friend compile;
private template: Template;
}
function compile(layout: Layout, environment: Environment): CompiledBlock {
return layout.template.compile(environment);
} Personal viewOne of the examples presented: // a.ts
import { update } from "./b";
export class A {
friend update;
private x: number;
}
// b.ts
import { A } from "./a";
export function update(a: A) {
a.x = 1;
} Has a cyclic dependency :-/ |
I've made a new proposal which could also solve this. Please check it out. Would love to hear some thoughts on it. #19151 |
For anyone struggling with this today, this is how I currently work around the issue. First I create an interface with the friendly methods and properties: export class B {
protected add(x: number, y: number): number {
return x + y;
}
}
interface FriendlyB {
add: (x: number, y: number) => number
} If possible, I keep the interface only in one single module without exporting it. let b = new B();
let theAnswer = ((b as any) as FriendlyB).add(20, 22);
console.log(theAnswer); Since the casting is compile-time, there is no overhead. |
It's been a few years, so any updates? I'd love to see this because the current best alternative is to make my private fields public, resulting in outsiders being able to access fields that they really shouldn't be accessing. |
NOTE: This proposal is an alternative to the proposal in #5228.
Overview
Often there is a need to share information on types within a program or package that should not be
accessed from outside of the program or package. While the
public
accessibility modifier allowstypes to share information, is insufficient for this case as consumers of the package have access
to the information. While the
private
accessibility modifier prevents consumers of the packagefrom accessing information from the type, it is insufficient for this case as types within the
package also cannot access the information. To satisfy this case, we propose the addition of a FriendDeclaration to classes:
A FriendDeclaration is a new declaration supported in the body of a ClassDeclaration or ClassExpression that indicates that the class, function, or namespace to which the supplied name resolves can treat the
private
andprotected
members of the class as if they were public.The name referenced in the FriendDeclaration is resolved relative to the ClassDeclaration. The name must be in scope.
Grammar
ClassElement: ( Modified )
FriendDeclaration
FriendDeclaration:
friend
FriendDeclarationNames;
FriendDeclarationNames:
EntityName
FriendDeclarationNames
,
EntityNameExamples
Instantiate a class with a private constructor:
Extend a class with a private constructor:
Access private members of an imported class:
The text was updated successfully, but these errors were encountered: