microsoft / microsoft/TypeScript
Bug: null can be excluded by !== null in flow before typeof === 'object', but not excluded by predicate function.
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
"null", "control flow", "narrowing", "narrow", "exclusion", "object", "typeof"
🕗 Version & Regression Information
Potentially, we could expect the behaviors of if (a === null) and if ((v => v !== null)(a)) (inferred as predicate since TS 5.5) should be the same, but it's actually not in specific cases.
If it's combined with typeof a === 'object' check in the same flow, first one is correctly omits the null possibility, while second one doesn't.
- This changed between versions:
- I confirmed that the exclusion in flow can be done since TS4.5. It's not regression, but the new feature seems to have been partially applied.
- TS4.4 https://www.typescriptlang.org/play/?ts=4.4.4#code/MYewdgzgLgBAlhAcgVwDapgXhgCgG4BcMyYA1mCAO5gCURe8EMYaGmAfDA5j86wNwAoAPTCY4mAD0A-INCRYAMyy4AhkRLkqtLJwDegifGU4EKdDlU0aMAE4BTKMltghRuCagBPAA72QyqowAIS8AOQgAEYAVvbAUGE2Dk4ubhKqbqJSsgC+QoJy4NAwAOYqlhpkFNQ2HDAG7iZBPNgs6EmOzq6GEh643n4BMEGh2BExcQkdKd1GGYZZMoJ5QA
- TS4.5 https://www.typescriptlang.org/play/?ts=4.5.5#code/MYewdgzgLgBAlhAcgVwDapgXhgCgG4BcMyYA1mCAO5gCURe8EMYaGmAfDA5j86wNwAoAPTCY4mAD0A-INCRYAMyy4AhkRLkqtLJwDegifGU4EKdDlU0aMAE4BTKMltghRuCagBPAA72QyqowAIS8AOQgAEYAVvbAUGE2Dk4ubhKqbqJSsgC+QoJy4NAwAOYqlhpkFNQ2HDAG7iZBPNgs6EmOzq6GEh643n4BMEGh2BExcQkdKd1GGYZZMoJ5QA
- This changed in commit or PR (not sure for now, sorry)
⏯ Playground Link
💻 Code
const isNull = (v: unknown) => v === null;
// ^?
const f = (a: unknown) => {
if (isNull(a)) return;
if (typeof a !== 'object') return;
a;
// ^?
};
const g = (a: unknown) => {
if (a === null) return;
if (typeof a !== 'object') return;
a;
// ^?
};
🙁 Actual behavior
Last a in f is inferred as object | null.
🙂 Expected behavior
Last a in f would be inferred as object.
Additional information about the issue
Here is important to check typeof === 'object' after the check for null, because type narrowing step is formally, unknown → object | null → object. === null alone cannot narrow anything on unknown.
If you swap the if-statements in above, you can see both is inferred as object.
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 các liên kết TypeScript Playground và các bản tái hiện tối thiểu cho f và g, so sánh các kiểu được suy luận sau bước kiểm tra typeof. Truy vết việc thu hẹp luồng điều khiển và hành vi của predicate được suy luận là nguyên nhân của sự khác biệt; được xem là hoàn tất khi trường hợp predicate thu hẹp a thành object không có null, với phạm vi kiểm thử hồi quy cho ví dụ đã báo cáo.
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