microsoft / microsoft/TypeScript
Type narrowing down on a union type with null results in incorrect type when applying `typeof`
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
type narrowing down, typeof
🕗 Version & Regression Information
I've tested all versions until 4.4.0 (Nightly)
⏯ Playground Link
💻 Code
type Container = { container: {type: "foo"} | {type: "bar"} | null }
function test(input: Container) {
if (input.container?.type === 'foo') {
// works
acceptsFoo(input.container);
acceptsFoo({type: "foo"})
// Does not works
acceptsFoo(input.container as typeof input.container)
acceptsFoo({type: "foo"} as typeof input.container)
/**
Error:
Argument of type '{ type: "foo"; } | null' is not assignable to parameter of type '{ type: "foo"; }'.
Type 'null' is not assignable to type '{ type: "foo"; }'.(2345) \
**/
}
}
function acceptsFoo(input: {type: "foo"}) {}
🙁 Actual behavior
input.container and input.container as typeof input.container are not equivalent.
🙂 Expected behavior
input.container and input.container as typeof input.container should be equivalent.
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
Tái hiện hành vi narrowing đã được báo cáo trong TypeScript Playground được liên kết bằng cách sử dụng Container và hàm kiểm thử được cung cấp. Bắt đầu bằng cách lần theo cách type-checker xử lý narrowing của thuộc tính tùy chọn và các typeof assertions; hoàn tất khi input.container và input.container as typeof input.container được xử lý tương đương mà không có nullability error.
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
- 35/100