microsoft / microsoft/TypeScript

Proposal: Friend Declarations for classes

Đang mở
#7,692 9 bình luận 69 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Awaiting More Feedback Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

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:
   friendFriendDeclarationNames;

  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;
}

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Issue không nêu tên tệp hay bài kiểm thử nào. Hãy bắt đầu bằng cách so sánh phương án thay thế này với proposal #5228 và xác thực ngữ pháp cùng các ví dụ dựa trên thiết kế ngôn ngữ và kiểm tra kiểu của TypeScript; được coi là hoàn tất khi đã thống nhất về một triển khai các khai báo friend bao quát quyền truy cập private và protected trong các trường hợp được nêu.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
compilers
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.