microsoft / microsoft/TypeScript

Strict version of Extract for literal union types

Đang mở
#31,474 1 bình luận 51 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.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

Search Terms

extract, pick, union, literal, string

Suggestion

Currently we have Pick<T, K> to select a type-safe subset of a given type T using a union of keys K. Unfortunately this does not work for unions of literals and requires Extract which subjectively isn't strict enough:

type ShirtSizes = "xxxl" | "xxl" | "xl" | "l" | "m" | "s" | "xs" | "xxs";
type Subset = Extract<ShirtSizes, "l" | "m" | "s" | "this-is-allowed-as-well">;

One caveat is that Extract does not support code completion when writing the literal union for the keys and it allows keys not in the first type as well. The former is error-prone while the latter is not causing a different behavior since the illegal keys are ignored but it can't be strictly checked in the long run. Of course I understand there are use cases wherein you'd like to use Extract more liberally so the definition

type Extract<T, U> = T extends U ? T : never;

is reasonable enough. Nevertheless, I'd like to propose an additional strict variant as follows

// short-hand form
type ExtractStrict<T, U extends T> = Extract<T, U>;
// equivalent full form
type ExtractStrict<T, U extends T> = T extends U ? T : never;

Use Cases

type ShirtSizes = "xxxl" | "xxl" | "xl" | "l" | "m" | "s" | "xs" | "xxs";
// this allows for code completion
type Subset = ExtractStrict<ShirtSizes, "l" | "m" | "s">;
// this is causing an error
type Subset2 = ExtractStrict<ShirtSizes, "l" | "m" | "s" | "illegal-literal">;

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.

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 xem xét utility type Extract hiện có và các ví dụ ShirtSizes của issue. Xác định một biến thể strict sẽ phù hợp với thiết kế hệ thống kiểu của TypeScript và hành vi kiểm thử như thế nào, bao gồm việc từ chối các literal không hợp lệ và hỗ trợ tính năng hoàn thành. Hoàn thành có nghĩa là kiểu được đề xuất đã được đặc tả, triển khai và kiểm thử bằng các compiler test.

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