docs: add type inference workarounds for object schemas#5751
Open
Iceshen87 wants to merge 1 commit intocolinhacks:mainfrom
Open
docs: add type inference workarounds for object schemas#5751Iceshen87 wants to merge 1 commit intocolinhacks:mainfrom
Iceshen87 wants to merge 1 commit intocolinhacks:mainfrom
Conversation
- Add comprehensive documentation for object property type inference - Provide 6 different solutions with examples - Explain root cause (TypeScript limitation, not Zod bug) - Add utility types: NonUndefined, NonNullable, ExtractProperty - Include test cases demonstrating all workarounds This addresses issue colinhacks#2654 by documenting the behavior and providing practical solutions for users encountering unexpected undefined in object property types. Fixes: colinhacks#2654 /claim colinhacks#2654
Author
|
Hi! Does Zod offer bounties for documentation improvements? If there's a bounty program, I'd appreciate if you could add the appropriate label. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds comprehensive documentation for a common type inference issue when using union types in Zod object schemas.
Problem
When using
.or()union types in object schemas, TypeScript infers an unexpected& (T | undefined)intersection:Root Cause
This is not a Zod bug but a TypeScript type inference limitation with mapped types and union types in object properties.
Solution
This PR provides:
1. Documentation (
docs/type-inference.md)2. Example Code (
examples/type-inference-workarounds.ts)NonUndefined,NonNullable,ExtractProperty3. Analysis (
ANALYSIS.md)Solutions Provided
NonUndefined<T>removes undefined from unionExtractProperty<T, K>for property extraction.merge()for complex objectsFiles Added
docs/type-inference.md- User-facing documentationexamples/type-inference-workarounds.ts- Complete code examplesANALYSIS.md- Technical analysis (for maintainers)Impact
Related Issue
Fixes: #2654
/claim #2654