microsoft / microsoft/TypeScript
Can we cut down on `Object.assign` overloads?
Chưa có ai nhận issue này.
- 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
Mô tả
We can convert unions into intersections (thanks @jcalz!), and we can model variadic args pretty well, so I figured I'd try to model Object.assign with a single overload. Here's what I came up with:
// Maps elements of a tuple to contravariant inference sites.
type MapContravariant<T> = {
[K in keyof T]: (x: T[K]) => void
}
type TupletoIntersection<T, Temp = MapContravariant<T>> =
// Ensure we can index with a number.
Temp extends Record<number, unknown>
// Infer from every element now.
? Temp[number] extends (x: infer U) => unknown ? U : never
: never;
declare function assign<T extends object, Rest extends object[]>(
x: T,
...xs: Rest
): T & TupletoIntersection<Rest>;
Unfortunately this doesn't quite give the right results on the following:
let asdf = assign({x: "hello"}, Math.random() ? {x: "hello"} : {z: true });
Currently the type of asdf is:
| ({ x: string; } & { x: string; z?: undefined; })
| ({ x: string; } & { z: boolean; x?: undefined; })
What a beautiful type! Unfortunately you can see that the second element of the union tries to intersect types with conflicting properties for x which is nonsense.
Additionally, @weswigham pointed out that this really won't work for the case where you started off with just a plain array whose elements are eventually spread into Object.assign.
I do wonder if there's anything better we can do here.
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
Issue không nêu file, test hay entry point nào. Hãy bắt đầu bằng cách tái hiện kiểu assign được đề xuất cùng các ví dụ về union và spread-array từ issue, sau đó lần theo cách Object.assign hiện đang được mô hình hóa trong bộ kiểm tra kiểu của TypeScript và các khai báo thư viện chuẩn. Hoàn thành nghĩa là có một hướng tiếp cận đã được review, xử lý được các trường hợp này mà không có các intersection xung đột.
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
- Cần làm rõ
- Mức phù hợp với người mới
- 20/100