microsoft / microsoft/TypeScript
Proposal: Friend Declarations for classes
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
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 allows
types 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 package
from 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:
class A {
friend createA;
private constructor() { }
}
function createA() {
return new A();
}
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 and protected 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 , EntityName
Examples
Instantiate a class with a private constructor:
class A {
friend createA;
private constructor() { }
}
function createA() {
return new A();
}
Extend a class with a private constructor:
class A {
friend B;
private constructor() { }
}
class B extends A {
}
Access private members of an imported class:
// 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;
}
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
该 issue 没有指出任何文件或测试。首先将此替代方案与提案 #5228 进行比较,并根据 TypeScript 的语言和类型检查设计验证语法与示例;完成的标准是就 friend 声明的实现达成一致,使其覆盖所示案例中的 private 和 protected 访问。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100