microsoft / microsoft/TypeScript

Incorrect union type inference for conjunction with strictNullChecks disabled

Đang mở
#31,789 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Bug Domain: check: Type Inference
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ả

TypeScript Version: 3.4.5

Search Terms: union type inference conjunction strictNullChecks

Code

function fn(x: number) {
  return x && 'a'
}

Expected behavior:
Return type of fn should be 0 | 'a'

Actual behavior:
Return type of fn is '' | 'a' if strictNullChecks is disabled

This is causing me an issue with this specific bit of code, where I'm checking window so my code runs both on the browser and on the client:

function fn(x: number): OrientationType | undefined {
  return window && (window.outerWidth > window.outerHeight ? 'landscape-primary' : 'portrait-primary')
}

I can replace the conjunction with a conditional expression, and I know that if strictNullChecks is disabled I should have a fallback for the return value but I'm posting here for the sake of trying to understand if this is working as intended. It also feels weird that my example compiles with strictNullChecks enabled but doesn't compile when it's disabled.

Playground Link: https://www.typescriptlang.org/play/#src=function%20fn(x%3A%20number)%3A%20OrientationType%20%7C%20undefined%20%7B%0D%0A%20%20return%20window%20%26%26%20(window.outerWidth%20%3E%20window.outerHeight%20%3F%20'landscape-primary'%20%3A%20'portrait-primary')%0D%0A%7D

Related Issues:

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện các ví dụ trong TypeScript Playground được liên kết với strictNullChecks bị tắt và được bật. Theo dõi cách trình kiểm tra kiểu suy luận biểu thức phép hội trong fn(x: number). Được xem là hoàn tất khi thiết lập bị tắt tạo ra kiểu trả về dự kiến 0 | 'a' mà không làm hỏng thiết lập được bật hoặc ví dụ window.

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

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.