Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect type inference for a field that is not optional, but has type of a union with undefined #524

Closed
vladislavsheludchenkov opened this issue Jan 22, 2025 · 1 comment · Fixed by #526
Labels
Status: Released It's now live. Type: Bug Inconsistencies or issues which will cause a problem for users or implementors.

Comments

@vladislavsheludchenkov
Copy link

Bug Report

Actual behavior

If there is a non-optional attribute in an object that has a type of something | undefined, the logic that is skipping undefined is not reflected in the type, but still works in runtime:

import { deepmerge } from 'deepmerge-ts'

const a: { foo: string } = { foo: 'bar' }
const b: { foo: number | undefined } = { foo: undefined }

const c: { foo: number | undefined } = deepmerge(a, b)

console.dir(c) // { foo: 'bar' }

That results in a compile-time type diverging from a runtime type (in the example, it's string in runtime and number | undefined in compile-time)

The issue is not there if the field is marked as optional:

const a: { foo: string } = { foo: 'bar' }
const b: { foo?: number | undefined } = { foo: undefined }

const c: { foo: string | number } = deepmerge(a, b)

Expected behavior

The logic that filters out undefined should be reflected in the type even if an optional field is represented via union, and not using a ?:

const a: { foo: string } = { foo: 'bar' }
const b: { foo: number | undefined } = { foo: undefined }

const c: { foo: string | number } = deepmerge(a, b)
@vladislavsheludchenkov vladislavsheludchenkov added Status: Triage This issue needs to be triaged. Type: Bug Inconsistencies or issues which will cause a problem for users or implementors. labels Jan 22, 2025
@RebeccaStevens RebeccaStevens removed the Status: Triage This issue needs to be triaged. label Jan 22, 2025
RebeccaStevens added a commit that referenced this issue Jan 23, 2025
Those using the `DeepMergeLeaf` type directly must now pass the `Fs` and `M` type parameters to this type.

fixes #524
github-actions bot pushed a commit that referenced this issue Jan 23, 2025
## [7.1.4](v7.1.3...v7.1.4) (2025-01-23)

### Bug Fixes

* apply filtering to types when selecting a leaf node ([#526](#526)) ([6d85163](6d85163)), closes [#524](#524)
Copy link

🎉 This issue has been resolved in version 7.1.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions github-actions bot added the Status: Released It's now live. label Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Released It's now live. Type: Bug Inconsistencies or issues which will cause a problem for users or implementors.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants