microsoft / microsoft/TypeScript
Partial type for reduce initialValue
Chưa có ai nhận issue này.
- 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
reduce initialvalue partial type assertion
Suggestion
Add a signature to the type for Array.reduce<U> to allow the initialValue to be a Partial<U>
Use Cases
Many cases where the reduce function is used to build an object that will eventually conform to U, but starts with an empty accumulator.
Examples
Simple example where the initialValue does not satisfy U but the final result will:
['one'].reduce<{ one: string }>((acc, i) => ({ [i]: i, ...acc }), {})
This yields
Argument of type '{}' is not assignable to parameter of type '{ one: string; }'.
Property 'one' is missing in type '{}' but required in type '{ one: string; }'.
Calling the function without an initialValue causes problems because the initialValue needs to be an object for this to work.
The below type signature would fix this issue:
interface Array<T> {
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: Partial<U>): U;
}
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 việc xem xét chữ ký Array.reduce được mô tả trong issue và ví dụ sử dụng một đối tượng rỗng làm initialValue. Kiểm tra cách tham số Partial được đề xuất ảnh hưởng đến tính an toàn kiểu và các overload reduce hiện có. Được xem là hoàn tất khi đề xuất được giải quyết với một thiết kế hệ thống kiểu đã được thống nhất và phần validation tương ứng.
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