Flexibility of template literal types for enums

Đang mở
#41,649 2 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ần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
typescript
Lĩnh vực
compilers

Hướng nghiên cứu

Bắt đầu bằng việc xem xét tương tác được đề xuất trong issue giữa template literal types và các cặp khóa/giá trị của enum. Payload không nêu tệp, test hoặc entry point nào, và cú pháp cũng như ngữ nghĩa vẫn còn mang tính suy đoán. Công việc chỉ được xem là hoàn tất khi có thiết kế đã được thống nhất cho các hoán vị của enum cùng một kế hoạch triển khai và xác thực rõ ràng.

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

Search Terms

template literal type enum permutations

Suggestion

Typescript 4.1 brought template literal types, which make it simple to express all the possibilities of permutations of unions of string types concatenated together. It would be cool if there was some equivalent syntax for expressing enum key/value pairs with the same level of expressivity that template literal types provide.

Use Cases

However, my codebase right now uses enums for these kinds of string types, so if I want to express a bunch of permutations of possible values with an enum, I can't really leverage template literal types in this case.

Examples

this is just some syntax I'm making up, I don't know how this would work especially for dealing with keys and values separately:

enum Color {
  RED,
  BLUE
}
enum Shape {
  SQUARE,
  CIRCLE
}
enum ColoredShape {
  [`${keyof Color}_${keyof Shape}`]
}

console.log(ColoredShape.RED_SQUARE, ColoredShape.BLUE_CIRCLE)

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
    • I think?
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.