microsoft / microsoft/TypeScript

Error from `exactOptionalPropertyTypes` not reported when using object spread with a ternary and optional property acces

Đang mở
#63,240 1 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: flag: exactOptionalPropertyTypes
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

exactOptionalPropertyTypes
object spread
ternary

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about exactOptionalPropertyTypes, object spread.
⏯ Playground Link

https://www.typescriptlang.org/play/?exactOptionalPropertyTypes=true&ts=5.9.3&ssl=5&ssc=32&pln=1&pc=1#code/C4TwDgpgBAYg9nKBeKBvAUFKYCGAnCAO2AEkATAfgC4oBnYPAS0IHN0BfddMiAYwBt80XnEL0oBAI4BXCPQBCcMiBrw4Abi78IwKMHwsdqhJvQB6M3QAWcAO60oEAB6RewCGUd48cPFDKyeogAok44bgDyYMCMojj8AAo+kHigACrgcuj6eIa6KKjYQsTkNFKyCkogAHS4BCWe7KYWUAC2jLS0zCxePn62VkRQ0l2sdGAEOJ44hJ7ueIT4INkGOshoUNVbABQMgRQbdUSkZGUQMnLAisq1xSdQ7FA0qOwAlA-qUC3yAKoA4lwRGJdIUjg0HutypdriBNDk8utCltqrs8PtDndSkV6vdHs83h8sF9LL8AeZLBBvL4oDgwJB8A4RN4+MB+CAoIwAGZQABu8UCHSghDguhw2GSlNANN4vDktA8UE5PlaNMIUDgACMAFYs9BA8Rgk4AJnWAHJTTSHPQmGMAD7DWYQTnMDxw1b5DbI1Ho0GY07Y47kE14tAEproIA

💻 Code
type Foo = {
  parentId?: string
}

declare const requestBody: Foo;

let target: Foo;

// shows expected error due to ExactOptionalPropertyTypes
target = { parentId: requestBody.parentId };

// missing error when using spread and ternary
target = { ...(true ? { parentId: requestBody.parentId } : {}) }; // BUG

const { parentId } = requestBody;
target = { ...(true ? { parentId: parentId } : {}) };   // BUG

// error appears correctly if value is not a property accessed from an object
const parentId2 = '' as string | undefined;
target = { ...(true ? { parentId: parentId2 } : {}) };

🙁 Actual behavior

The expected error is silenced only in the specific case that combines object spread, ternary, and accessing an optional property from an object, although the types are always recognized correctly with | undefined.

🙂 Expected behavior

All scenarios demonstrated in the code snippet are expected to show the same error.

Additional information about the issue

No response

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 TypeScript Playground được liên kết và chạy bản tái hiện exactOptionalPropertyTypes. So sánh các trường hợp gán trực tiếp, toán tử ba ngôi với object spread, thuộc tính được destructure và biến cục bộ; issue được hoàn tất khi các trường hợp spread và toán tử ba ngôi báo cáo cùng một lỗi như mong đợi với các kịch bản 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ó
3/5
Thời gian dự kiến
1-2 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.