microsoft / microsoft/TypeScript
keyof on generics arbitrarily returns `string` rather than the inferred values depending on the generic
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ả
Bug Report
🔎 Search Terms
keyof generic string
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about it
⏯ Playground Link
Playground link with relevant code
💻 Code
export type Item = {
args?: Record<string, string>;
};
export type BaseSpec = Record<string, {
items: Item[];
}>;
export function genericFunction<Spec extends BaseSpec>(spec: Spec): keyof Spec {
return {} as any;
}
const AAAAA: `someKey` = genericFunction({
someKey: {
items: [{
args: {},
}, {
args: {
name: `foo`,
},
}],
},
});
🙁 Actual behavior
Type 'string' is not assignable to type '"someKey"'.
🙂 Expected behavior
No errors
💻 Additional Information
Weirdly, it seems to work if all args objects that are specified have exactly the same content. Removing an arg object seems to work, and it works as well if we add name: "foo" into the first arg object (or remove it from the second one).
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
Mở TypeScript Playground được liên kết và tái hiện lỗi với genericFunction, BaseSpec và các đối tượng args khác nhau. Điều tra lý do keyof Spec được mở rộng thành string trong trường hợp suy luận generic này, sau đó xác minh rằng ví dụ vượt qua kiểm tra kiểu và kết quả được suy luận là "someKey".
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
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100