microsoft / microsoft/TypeScript

Object is possibly undefined after undefined check on readonly property

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

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

Experience Enhancement Suggestion
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.7.4, 3.9.2

Search Terms:
Object is possibly undefined
undefined readonly callback
Code

class Test {
  private readonly testProperty?: { value: number };
  public constructor(testProperty: { value: number }) {
    this.testProperty = testProperty;
  }

  public method() {
    if (this.testProperty) {
      Promise.resolve().then(() => this.testProperty.value); //Object is possibly undefined
    }
  }
}

Expected behavior:
testProperty should not be resolved as possibly undefined as this is not possible in this scenario as the property has already been checked and it is marked as readonly.

Actual behavior:
testProperty is possibly undefined, requiring another undefined check.

Playground Link:
https://www.typescriptlang.org/play/#code/MYGwhgzhAEAqCmEAu0DeAoa0AOAnAlgG5hLzS7xgAmA9gHYgCe0pyACrjdvLkowPwAuNNGIgArvGF1xAWwBGPaAF8A3Jhzj5IfMGjB6yXOOBIauABSskHLjz7DUosBKnQZCpcoCUaDViQAC3wIADprW25eZgBeFkQbTii+dSxldA1sLR09WXggmioLXwwsLHwAM2grYLCIpPtGEv8y21kQ+FCKCBoQQnhi8MD4Ogti6BiAPhZa8ITIxtCxSW9VaAB6dYB5eQAreFNoEJwaKHxtZnE6KngK-Dp4Khb0tPRlIA

Related Issues:

Similar issues but these i understand why TypeScript would report an error, in my case the property is readonly so it seems like a bug to me.
https://github.com/microsoft/TypeScript/issues/38053
https://github.com/microsoft/TypeScript/issues/11498

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 với bản tái hiện được liên kết trong Playground và so sánh hành vi được mô tả trong các issue liên quan #38053 và #11498. Theo dõi việc kiểm tra kiểu cho readonly optional property bên trong callback của Promise; hoàn tất khi ví dụ được cung cấp không còn báo cáo “possibly undefined” hoặc hành vi mong đợi được giải quyết theo cách khác.

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

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.