microsoft / microsoft/TypeScript
Spreading object with optional property causes incorrect type inference when `exactOptionalPropertyTypes = false`
Chưa có ai nhận issue này.
- 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
spread optional property undefined
🕗 Version & Regression Information
5.9.3 and others
⏯ Playground Link
💻 Code
const a = { x: 42 }
const b1 = { x: undefined }
const c1 = { ...a, ...b1 } // type of c1 is inferred as { x: undefined; } - that's correct ✅
console.log(c1)
const b2: { x?: number } = { x: undefined } // this does not produce an error when exactOptionalPropertyTypes=false
const c2 = { ...a, ...b2 } // type of c2 is inferred as { x: number; } - that's wrong, as c2.x _is_ undefined ❌
console.log(c2)
🙁 Actual behavior
I know that there have been several issues filed before like https://github.com/microsoft/TypeScript/issues/57086 / https://github.com/microsoft/TypeScript/issues/51755 / https://github.com/microsoft/TypeScript/issues/51253 / https://github.com/microsoft/TypeScript/issues/57408 and they all were closed referring to exactOptionalPropertyTypes.
However, reading the docs, the description of exactOptionalPropertyTypes says:
exactOptionalPropertyTypes makes TypeScript truly enforce the definition provided as an optional property:
const settings = getUserSettings();
settings.colorThemeOverride = "dark";
settings.colorThemeOverride = "light";// But not:
settings.colorThemeOverride = undefined;Type 'undefined' is not assignable to type '"dark" | "light"' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the type of the target.
So, the option changes the way, how ?: is defined from "can be not present or undefined" to "can be not present, but not set to undefined".
But even when exactOptionalPropertyTypes=false, I don't see a reason why TS should infer an invalid type in the example given above. It knows that x can be undefined (or not present) and it knows that exactOptionalPropertyTypes=false, so it should really infer in this case that the prop value can be undefined. For me, that would appear to be a consistent implementation of the language semantics.
Or do I miss something?
🙂 Expected behavior
I want to rely on TS inferring correct types, also in this case.
Additional information about the issue
Of course, this change would be a breaking change, so there could be another option to activate it.
We have a large project and there are many places (including JSON stored in customer DBs) that would need to be adjusted if we want to set exactOptionalPropertyTypes to true. So that's not a solution we can quickly implement.
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
Bắt đầu với ví dụ TypeScript Playground được liên kết và so sánh các kiểu được suy luận của nó với các thiết lập exactOptionalPropertyTypes khác nhau. Đọc các issue liên quan #57086, #51755, #51253 và #57408 để hiểu các quyết định trước đây. Công việc được xem là hoàn tất khi kết quả spread trong trường hợp được báo cáo phản ánh rằng x có thể là undefined mà không yêu cầu bật exactOptionalPropertyTypes.
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ó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- 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