{ [Symbol.toPrimitive]: () => Symbol }: support as index/property type

Đang mở
#43,428 1 bình luận 3 reaction 0 người được giao Xem trên GitHub

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

Đánh giá

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

Hướng nghiên cứu

Bắt đầu bằng việc xem xét ba chẩn đoán kiểm tra kiểu đã được báo cáo đối với tên thuộc tính được tính toán và kiểu chỉ mục trong ví dụ làm động lực. Được xem là hoàn tất khi các giá trị có các phương thức [Symbol.toPrimitive] được chấp nhận trong các interface, tên thuộc tính và biểu thức chỉ mục, đồng thời vẫn giữ nguyên hành vi kiểm tra kiểu được yêu cầu.

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

Mô tả

Awaiting More Feedback Suggestion

Suggestion

🔍 Search Terms

  • Symbol.toPrimitive
  • unique symbol

✅ 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.

⭐ Suggestion

  1. Instead of receiving A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type. I want to be able to use types with [Symbol.toPrimitive] methods as properties in an interface.
  2. Instead of receiving A computed property name must be of type 'string', 'number', 'symbol', or 'any'. I want to be able to use a value with a [Symbol.toPrimitive] method as a property name.
  3. Instead of receiving Type 'SomeType' cannot be used as an index type. I want to be able to use a value with a [Symbol.toPrimitive] method as an index type.

📃 Motivating Example

class UniquePerson {
  #symbol = Symbol();
  constructor(readonly name: string) {}
  [Symbol.toPrimitive]() {
    return this.#symbol;
  }
}
const sherlock = new UniquePerson("Sherlock Holmes");
interface PersonToAddress {
  [sherlock]: string;
  // ^ A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.
}
const personToAddress: PersonToAddress = {
  [sherlock]: "221B Baker Street",
  // ^ A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
};
personToAddress[sherlock]; //=> 221B Baker Street
               // ^ Type 'UniquePerson' cannot be used as an index type.

💻 Use Cases

I am working on a library that heavily uses Symbols to add extension function to other types while avoiding name conflicts (as Symbols are unique). Instead of passing around Symbols directly I want to pass around richer objects with more information but, when passed in as an index type, I want the [Symbol.toPrimitive] functions on my richer objects to be called and resolve to the appropriate Symbol. Currently I am exploring either not supporting TypeScript initially (which is not ideal and could inhibit adoption) or ditch the idea of passing around richer objects and use Symbols directly (but this greatly reduces the usability and usefulness of the library).

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

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.

Issue khác của microsoft/TypeScript

Tất cả issue của microsoft/TypeScript

Issue tương tự

Thêm issue về Go

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.