Skip to content

fix(schema): preserve array element inference#46

Merged
SrHenry merged 3 commits into
developerfrom
fix/array-schema-inference
May 27, 2026
Merged

fix(schema): preserve array element inference#46
SrHenry merged 3 commits into
developerfrom
fix/array-schema-inference

Conversation

@SrHenry

@SrHenry SrHenry commented May 27, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes #39Infer<typeof array(schema)> now correctly resolves to the element type array (e.g. string[]) instead of {}[].

Root Cause

The (tree: {}) overload in ArraySchema was positioned before the generic <T>(tree: ValidatorMap<T> | TypeGuard<T> | StandardSchemaV1<T, T>) overload. Since FluentSchema<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-all
  • src/validators/schema/array.ts: Same reorder in _fn and OptionalizedArray type overloads
  • src/validators/__tests__/array.spec.ts: New test file with type inference assertions and runtime regression checks

Verified Inference

Expression Before After
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 --noEmit passes
  • yarn tsc -p tsconfig.cjs.json --noEmit passes
  • yarn build succeeds
  • All 373 existing tests pass
  • New type inference tests pass (6 type assertions + 4 runtime checks)
  • No circular dependencies
  • Lint/format clean on changed files

SrHenry added 2 commits May 27, 2026 01:40
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.
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
@SrHenry SrHenry merged commit f2c15b9 into developer May 27, 2026
2 checks passed
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.

fix: Infer<FluentSchema<T[], ...>> resolves to {}[] instead of T[] — GetTypeGuard fails on array-typed FluentSchema

1 participant