fix(schema): preserve array element inference#46
Merged
Conversation
Reorder ArraySchema overloads specific-before-broad so that
TypeGuard/ValidatorMap/StandardSchemaV1 overloads match before the
catch-all (tree: {}) overload. Same reorder in _fn and OptionalizedArray.
Closes #39
Cover array(string()).optional() (known: |undefined dropped), asEnum literal union inference, and custom TypeGuard inference.
This was referenced May 27, 2026
Infer<T> correctly preserves | undefined for FluentOptionalSchema. Previous test asserted the broken behavior (string[] instead of string[] | undefined) based on misleading TypeScript error messages. Closes #48
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
Fixes #39 —
Infer<typeof array(schema)>now correctly resolves to the element type array (e.g.string[]) instead of{}[].Root Cause
The
(tree: {})overload inArraySchemawas positioned before the generic<T>(tree: ValidatorMap<T> | TypeGuard<T> | StandardSchemaV1<T, T>)overload. SinceFluentSchema<T>extends{}, TypeScript always matched the broad(tree: {})overload first, losing the element type.Changes
src/validators/schema/types/ArraySchema.ts: Reorder overloads — generic overload before(tree: {})catch-allsrc/validators/schema/array.ts: Same reorder in_fnandOptionalizedArraytype overloadssrc/validators/__tests__/array.spec.ts: New test file with type inference assertions and runtime regression checksVerified Inference
Infer<typeof array(string())>{}[]string[]Infer<typeof array(object({ id: string() }))>{}[]{ id: string }[]Infer<typeof array({ id: string() })>{ id: StringSchema }[]{ id: string }[]Infer<typeof array(zodStdSchema)>{}[]string[]Infer<typeof array()>unknown[]any[]No runtime behavior changes — only type-level resolution is affected.
Test Plan
yarn tsc -p tsconfig.json --noEmitpassesyarn tsc -p tsconfig.cjs.json --noEmitpassesyarn buildsucceeds