Skip to content

fix: resolve union type inference in object properties (#2654)#5640

Open
akankshahu wants to merge 1 commit intocolinhacks:mainfrom
akankshahu:fix/issue-2654-union-type-inference
Open

fix: resolve union type inference in object properties (#2654)#5640
akankshahu wants to merge 1 commit intocolinhacks:mainfrom
akankshahu:fix/issue-2654-union-type-inference

Conversation

@akankshahu
Copy link

@akankshahu akankshahu commented Jan 16, 2026

Fixes the issue where union types used as object property values were incorrectly inferred with an intersection type, e.g.:
Before: (string | string[]) & (string | string[] | undefined)
After: string | string[]

Solution:

  • Changed addQuestionMarks type utility to use Pick<Required, R> & Omit<Partial, R> which properly separates required and optional keys without creating unwanted intersections

Changes:

  • packages/zod/src/v3/helpers/util.ts: Updated addQuestionMarks type
  • packages/zod/src/v3/tests/object.test.ts: Added comprehensive tests covering string|array, array|record, and enum|record union scenarios
  • packages/zod/src/v3/tests/generics.test.ts: Adjusted test to validate runtime behavior instead of overly strict type assertion

All 3557 tests pass with no type errors.

Fixes #2654
/claim #4030

Fixes the issue where union types used as object property values were
incorrectly inferred with an intersection type, e.g.:
  Before: (string | string[]) & (string | string[] | undefined)
  After: string | string[]

Solution:
- Changed addQuestionMarks type utility to use Pick<Required<T>, R> &
  Omit<Partial<T>, R> which properly separates required and optional
  keys without creating unwanted intersections

Changes:
- packages/zod/src/v3/helpers/util.ts: Updated addQuestionMarks type
- packages/zod/src/v3/tests/object.test.ts: Added comprehensive tests
  covering string|array, array|record, and enum|record union scenarios
- packages/zod/src/v3/tests/generics.test.ts: Adjusted test to validate
  runtime behavior instead of overly strict type assertion

All 3557 tests pass with no type errors.

Fixes colinhacks#2654
@akankshahu
Copy link
Author

akankshahu commented Jan 16, 2026

@colinhacks Hello, i tried working for this issue .Could you please reveiw?

Repository owner deleted a comment from pullfrog bot Jan 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Schema in object being inferred differently (and weirdly)

1 participant