Incorrect type inference for nested optional properties
Status: ReleasedType: Bug
## Bug Report
Inferred type isn't as strict as it could be when merging an object with nested properties with the same object, but with properties declared as optional at all levels (like `Partial`, but recursive). All properties deeper than top-level become optional in the result, while they're actually always present (we know that from the first object type)
### Actual behavior
```ts
const a: { outer: { inner: number } } = { outer: { inner: 1 } }
const b: { outer?: { inner?: number } } = {}
const c = deepmerge(a, b) // inferred to { outer: { inner?: number }}
console.log(c) // outputs { outer: { inner: 1 } }
```
Interestingly, the issue is not present when providing the same object twice in the arguments list:
```ts
const a: { outer: { inner: number } } = { outer: { inner: 1 } }
const b: { outer?: { inner?: number } } = {}
const c = deepmerge(a, a, b) // inferred to { outer: { inner: number }}
```
### Expected behavior
I expect the result to be inferred as non-partial at all levels where we have at least one type declaring the properties as mandatory:
```ts
const a: { outer: { inner: number } } = { outer: { inner: 1 } }
const b: { outer?: { inner?: number } } = {}
const c = deepmerge(a, b) // inferred to { outer: { inner: number }}
```
关闭于 2025-02-23 1 条评论