Incorrect inferred function return type with destructuring assignments
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả 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
Bắt đầu với repro TypeScript Playground được cung cấp và so sánh các trường hợp 1–4, tập trung vào việc suy luận kiểu trả về generic thông qua các phép gán destructuring. Truy tìm lý do trường hợp 4 mở rộng kết quả thành R, sau đó xác minh rằng A3.a không còn báo lỗi null hoặc undefined, trong khi các trường hợp khác vẫn không thay đổi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
🔎 Search Terms
"return type destructure"
"destructuring assignment type"
🕗 Version & Regression Information
- This changed between versions 3.3.3 and 3.5.1
⏯ Playground Link
💻 Code
type R = {
A: {
a: 'A'
} | null | undefined
}
function fn<T extends R>(t:T): T {
return {} as T;
}
/*
In each test case below, I'd assume the return type of function to be `{A: {a: 'A'}}`.
However, case 4 uses destructuring and this appears to default the return type to `R`.
*/
// Case 1
// Correctly infers type of `result.A` as `{A: {a: 'A'}}`
const result = fn({A: {a: 'A'}});
result.A.a; // No error
// Case 2
// Correctly infers type of A1 as `{a: 'A'}`
const A1 = fn({A: {a: 'A'}}).A;
A1.a; // No error
// Case 3
// Correctly infers type of A3 as `{a: 'A'}`
const {A: A2} = fn({A: {a: 'A'}} as const);
A2.a; // No error
// Case 4
// Incorrectly infers type of A2 as `{A: {a: 'A'} | null | undefined}`
const {A: A3} = fn({A: {a: 'A'}});
A3.a; // Error: Object is possibly `null` or `undefined`
🙁 Actual behavior
In case 4, accessing A3.a results in error "Object is possibly null or undefined".
Appears that the inferred type of A3 is {a: 'A'} | null | undefined. I suspect when we destructure, the return type of fn is inferred as R instead of the narrowed generic type variable T.
🙂 Expected behavior
Expect no errors. Should be able to access A3.a, similar cases 1, 2, and 3. Expectation is that destructuring assignment shouldn't modify inferred return type of function.
Additional information about the issue
Current workarounds:
- Use
as constappended to param (see case 3) - Use
constwith generic type variable in function signature (function fn<const T extends R>(t: T): T;) - Avoid destructuring assignments (case 1 & 2)
- 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
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ự
-
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
caddyserver/caddy#8046 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
-
L1 recommended for recruits
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
area/entangle bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100