microsoft / microsoft/TypeScript
Spread operator allows invalid return type in record data types
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
spread operator, return type mismatch on record of functions
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries. Nothing related was mentioned.
I've tried 3.3, 4.0, 5.0 and 5.8.3 and got the same result.
⏯ Playground Link
💻 Code
type Fn<I, O> = { type: "fn"; fn: (input: I) => O };
type FnRecord<I, O> = Record<string, Fn<I, O> | undefined>;
function toFnRecord<I, O>(
actions: Record<string, (input: I) => O | undefined>
): FnRecord<I, O> {
throw new Error();
}
const t: FnRecord<void, number> = {
a: { type: "fn", fn: () => 2 }, // <- commenting out this line results in an error in the line below
...toFnRecord({ b: () => "3" }),
}
🙁 Actual behavior
No error was reported
🙂 Expected behavior
Expect the following type mismatch error message. This is reported if I comment out the entry without spread operator (a: { type: "fn", fn: () => 2 },)
Type '{ [x: string]: Fn<void, string> | undefined; }' is not assignable to type 'FnRecord<void, number>'.
'string' index signatures are incompatible.
Type 'Fn<void, string> | undefined' is not assignable to type 'Fn<void, number> | undefined'.
Type 'Fn<void, string>' is not assignable to type 'Fn<void, number>'.
Type 'string' is not assignable to type 'number'.(2322)
Additional information about the issue
Similar to #61754, but this example does not involve any explicit generic subtyping constraints (extends).
I'm not sure if this is related to #10727.
I'd like to know any workaround that can prevent this type error happening at runtime. I've run into this multiple times with the same pattern used in my codebase and got very surprised each time.
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 được liên kết trong TypeScript Playground và so sánh các chẩn đoán khi có và không có mục không dùng spread. Điều tra cách kết quả spread được kiểm tra đối với FnRecord<void, number>; hoàn tất khi kiểu trả về string không hợp lệ tạo ra lỗi mismatch như mong đợi, trong khi các giá trị record hợp lệ vẫn được chấp nhận.
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
- 35/100