microsoft / microsoft/TypeScript
Preserve tuples when mapping with `as` clauses
Đang mở
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ả
Search Terms
#tuple #mapped #as #clause #preserve
Suggestion
New mapped type's as clause allows one to filter the entries of an object. Basic mapped types preserve the shape of a tuple but not when used with the new as clause:
type tuple = [0, 1, 2]
type Mapped<T> = {
[K in keyof T]: T[K] | undefined
}
type MappedAs<T> = {
[K in keyof T as K]: T[K] | undefined
}
type test0 = Mapped<tuple> // tuple shape is preserved
type test1 = MappedAs<tuple> // tuple shape is object now
test0 === [0 | undefined, 1 | undefined, 2 | undefined]
test1 === {
[x: number]: 0 | 1 | 2 | undefined;
0: 0 | undefined;
1: 1 | undefined;
2: 2 | undefined;
length: 3 | undefined;
toString: (() => string) | undefined;
toLocaleString: (() => string) | undefined;
...
}
Use Cases
- Filtering lists without having to rely on costly recursive types
Examples
type FilterNumbers<T> = {
[K in keyof T as T[K] extends number ? never : K]: T[K] | undefined
}
type test2 = FilterNumbers<[1, '2', 3, '4']> // ['2', '4']
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
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- 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 tái hiện các ví dụ tuple trong issue bằng TypeScript và so sánh kết quả của mapped types khi có và không có mệnh đề as. Điều tra cách compiler xử lý mapped types, sau đó bổ sung coverage cho thấy việc lọc một tuple vẫn giữ nguyên hình dạng tuple và xác minh rằng ví dụ tạo ra tuple đã lọc như mong đợ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
- 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
- 25/100