microsoft / microsoft/TypeScript

Unsafe Map overload spoils direct property access on parenthesized assignment expression

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

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

Domain: check: Type Inference Help Wanted Possible Improvement
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ả

🔎 Search Terms

map overload any unknown unsafe weakmap set binary assignment lazy init

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250326#code/DYUwLgBAxghlAWIBcECyMAOAeAzmATgJYB2A5gDQQD2ARgFYhRgB8EAPhAK7EAmIAZiRA8A3ACgx-bk0JViEMCDwBGABQBrEAE8UeImQCUEAN5iI0OXgj4lnYJAC8EVbAQgIAfg9PiIAO5omKoGBgB0pOAa2gbi5gD0cebmAHoeZtbgnPjyNjh2YOIAvhJSxDJyCkpgAExROhB6JKRGpuauiJ7eEL4B6BjBsRbEVrn5EE7tIOGRmlox6QlJEKnpNmBZObb2RWJAA

💻 Code
let cache: Map<string, object> | undefined;

function test1(key: string) {
  const result = (cache ??= new Map()).get(key);
  //    ^? const result: any
  return result;
}

function test2(key: string) {
  cache ??= new Map();
  const result = cache.get(key);
  //    ^? const result: object | undefined
  return result;
}
🙁 Actual behavior

First result gets spoiled as any

🙂 Expected behavior

I'd expect both results to be object | undefined

Additional information about the issue

This bug reports an issue related to Map but, tbf, it's not unique to Map and a more general solution would have to be found to properly fix this. The problem is how checkBinaryLikeExpressionWorker create a union of left and right types here - without taking into consideration that the assignment target might already overlap with the right type. That union type gets reduced with UnionReduction.Subtype and .get gets read from it - instead of the assigned to reference on the left side.

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

Tái hiện sự khác biệt giữa test1 và test2 bằng TypeScript Playground được liên kết, sau đó kiểm tra checkBinaryLikeExpressionWorker, thành phần được báo cáo xác định là nơi tạo union của các toán hạng phép gán. Theo dõi cách union đó được rút gọn trước khi .get được phân giải. Được xem là hoàn tất khi truy cập phép gán có đặt trong ngoặc suy luận ra object | undefined giống như dạng phép gán riêng biệt, kèm theo 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
45/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.