microsoft / microsoft/TypeScript
implicit 'any' in method with parameter of inferred generic type
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ả
🔎 Search Terms
implicit 'any' method parameter inferred generic type
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about type inference
⏯ Playground Link
💻 Code
type Constructor<T> = {
new (...args: never[]): T;
};
interface GenericParams<T> {
bar: (t: T) => void;
}
type List<T extends readonly {}[]> = {
[K in keyof T]: [Constructor<T[K]>, GenericParams<T[K]>];
};
declare function foo<T extends readonly {}[]>(params: List<[...T]>): void;
class Foo {
test() {}
}
class Bar {}
// Inference fails
foo([
//^?
[Foo, {bar(t) {
}}],
[Bar, {bar(t) {
}}]
])
// Inference works
foo([
[Foo, {bar(t) {
t.test();
}}]
])
interface NumberParams<T> {
bar: (t: number) => void;
}
type List2<T extends readonly {}[]> = {
[K in keyof T]: [Constructor<T[K]>, NumberParams<T[K]>];
};
declare function foo2<T extends readonly {}[]>(params: List2<[...T]>): void;
// Inference works
foo2([
// ^?
[Foo, {bar(t) {
}}],
[Bar, {bar(t) {
}}]
])
🙁 Actual behavior
In the foo example, the parameter t in the associated bar methods are inferred as type any. It seems that TypeScript should be able to infer the actual types of the parameters.
🙂 Expected behavior
t is inferred as type T in each case, i.e. t should be Foo in the Foo case, and Bar in the Bar case.
There are two additional cases provided here: one with a single element in the provided array, and one where the parameters are not generic (number). In both cases, type inference does assign the correct non-any type. It's unclear what exactly the issue is in the first case.
Additional information about the issue
It's possible this is related to #47599, though I don't have enough knowledge of the internals to say whether or not it's the same class of bug. I haven't found another issue reporting this same behaviour.
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 với bản tái hiện TypeScript Playground được liên kết và so sánh các lệnh gọi foo generic với các trường hợp một phần tử và non-generic của foo2. Theo dõi hành vi suy luận kiểu xung quanh kiểu List được ánh xạ; hoàn thành khi mỗi tham số bar được suy luận là Foo hoặc Bar thay vì any, trong khi các trường hợp khác vẫn chính xác.
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
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 38/100