fix(drizzle-zod): default TCoerce to undefined in createSchemaFactory#5973
Open
tsushanth wants to merge 1 commit into
Open
fix(drizzle-zod): default TCoerce to undefined in createSchemaFactory#5973tsushanth wants to merge 1 commit into
tsushanth wants to merge 1 commit into
Conversation
When no coerce option is passed to createSchemaFactory, TypeScript falls back to the full constraint bound for TCoerce rather than undefined. This causes GetZodType to distributively return a union of coerced and non-coerced types (e.g. ZodCoercedString | ZodString), which makes the BuildRefineField callback parameter unresolvable — TypeScript widens to any (ts 7006). Adding = undefined as a default type parameter matches the TCoerce used by the top-level createInsertSchema/createSelectSchema/createUpdateSchema exports, restoring proper type inference for refinement callbacks when createSchemaFactory is used without a coerce option.
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.
Fixes #5968
Root cause
createSchemaFactoryhas a genericTCoercewith no default:When the caller omits the
coerceoption (e.g.createSchemaFactory({ zodInstance: z })), TypeScript cannot inferTCoercefrom the argument and falls back to the full constraint bound:Partial<Record<...>> | true | undefined.This union propagates into
GetZodType, whereCanCoerce<TCoerce, 'string'>distributes over the union and returnsboolean. The conditional type then producesz.ZodCoercedString | z.ZodStringfor string columns.BuildRefineFieldis distributive over its input, so it produces two function overloads with incompatible parameter types:TypeScript cannot contextually type a callback against a union of function types with different parameter types, so
schemawidens toany(ts 7006).The top-level
createInsertSchema/createSelectSchema/createUpdateSchemaexports explicitly useCreateInsertSchema<undefined>, which is why they work correctly.Fix
Add
= undefinedas the default type parameter forTCoerceincreateSchemaFactory. When nocoerceoption is provided,TCoercenow defaults toundefinedinstead of widening to the full constraint bound — matching the behaviour of the top-level exports.