microsoft / microsoft/TypeScript

Make Arrays in public API readonly

Đang mở
#42,481 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

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

In Discussion Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

Suggestion

🔍 Search Terms

API readonly
getchildren readonly

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

This might actually break users of TypeScript's API. But so did the last change to make all properties on Node readonly.
Ideally users only need to adjust their types or create a slice of the array.

⭐ Suggestion

Node#getChildren() should return a readonly Node[].
UnionOrIntersectionType#types should be readonly Type[]
TypeChecker#getBaseTypes(type) should return readonly BaseType[] | undefined
TypeChecker#getPropertiesOfType(type) should return readonly Symbol[]
Symbol#declarations should be readonly Declaration[], well actually it should be readonly Declaration[] | undefined, but that's a different long-standing issue

... just to name a few. There are more throughout the API.

probably a stretch goal: make every property on Type, Symbol, etc. readonly (for API users)

📃 Motivating Example

The current types allow API users to mutate data structures they shouldn't be mutating. This might cause severe misbehavior down the road. E.g. EndOfFileToken#getChildren() might return emptyArray. If I modify this array, everything will break, because TypeScript reuses this array instance literally everywhere.

On a related note: It might make sense to change to const emptyArray: readonly never[]. Though that will probably cause a few (reasonable) breaks in the compiler itself.

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

Bắt đầu bằng cách đọc các khai báo API public cho Node#getChildren, UnionOrIntersectionType#types, TypeChecker#getBaseTypes, TypeChecker#getPropertiesOfType và Symbol#declarations. Tìm các thuộc tính mảng public khác và xem xét việc sử dụng emptyArray. Được xem là hoàn tất khi các mảng được expose phù hợp là readonly, trong khi hành vi JavaScript lúc runtime vẫn không thay đổi.

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
api, compilers
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/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.