Suggestion: use receiver's declared type arguments as defaults to its class constructor
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
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- typescript
- Lĩnh vực
- compilers
Hướng nghiên cứu
Không có tệp, bài kiểm thử hoặc điểm vào của trình biên dịch nào được nêu. Hãy bắt đầu bằng cách xem xét các ví dụ GenericClass và Map, sau đó theo dõi cách các đối số kiểu của hàm khởi tạo được suy luận từ biểu thức nhận. Công việc được xem là hoàn tất khi đã xác định được cách xử lý các giá trị mặc định theo ngữ cảnh, các lời gọi gián tiếp, các lớp dẫn xuất và ngữ nghĩa của hàm khởi tạo.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
I run into this relatively simple pattern a lot:
class GenericClass<T, U, V> {
a: T;
b: U;
c: V;
}
let x: GenericClass<number, string, boolean>;
x = new GenericClass();
// Which produces the error:
// Error: GenericClass<{}, {}, {}> is not assignable to GenericClass<number, string, boolean>
Since x has already been defined with a particular set of type arguments, these arguments could be implicitly assumed as defaults to its type's constructor when invoked with it being the receiver (for simplicity I'm assuming here it is the exact constructor as declared e.g. not a derived class).
I understand there will be cases where that would be very difficult or even impossible to achieve:
let createInstance = () => new GenericClass()
x = createInstance();
but these are relatively rare. Most of the time it's just:
class Example {
myMap: Map<string, Array<{n: number}>>;
constructor() {
this.myMap = new Map<string, Array<{n: number}>>(); // <-- Why repeat?
}
}
Which instead could have been:
class Example {
myMap: Map<string, Array<{n: number}>>;
constructor() {
this.myMap = new Map(); // <-- Better.. :)
}
}
- Any possible issues with this? perhaps ones that I'm not aware of or haven't thought of?
- Potential name collisions? (though I'm not aware of any?)
- Maybe just a bit too difficult to implement? perhaps revisit later? (though #5256 appears more difficult than this and seems to be in consideration?)
- Or maybe this 'bends' the semantics of the constructor expression (
new Class<T>()), or at least its default type arguments, in a way that's slightly unappealing from a "purist" point of view"? (though this doesn't seem like a "purist" language?). Perhaps some concept of "contextual baseline default type arguments" can be included as an integral part of the future proposal for default type arguments?
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.4k
- Merge trung bình
- 1 ngày 15 giờ
- Pull request đã merge (30 ngày)
- 106
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.
Issue khác của microsoft/TypeScript
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
microsoft/TypeScript#64322 · 2 bình luận · 1 reaction · 2 người được giao ·
-
Possible Improvement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
microsoft/TypeScript#64278 · 1 bình luận · 1 reaction ·
-
Docs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
microsoft/TypeScript#64118 · 1 bình luận ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
microsoft/TypeScript#64094 ·
-
Docs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
microsoft/TypeScript#63959 · 5 bình luận ·
Tất cả issue của microsoft/TypeScript
Issue tương tự
-
Type/Bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
OpenNSW/nsw-srilanka#497 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
milvus-io/birdwatcher#545 ·
-
kind/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
kubernetes-sigs/prow#953 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
caddyserver/caddy#8046 ·