Skip to content

fix(form-core): exclude undefined from FormState errors type#2066

Open
isaackaara wants to merge 1 commit intoTanStack:mainfrom
isaackaara:fix/form-errors-type-exclude-undefined
Open

fix(form-core): exclude undefined from FormState errors type#2066
isaackaara wants to merge 1 commit intoTanStack:mainfrom
isaackaara:fix/form-errors-type-exclude-undefined

Conversation

@isaackaara
Copy link

The runtime code in FormApi.ts explicitly filters out undefined values from the error map when building the errors array:

fieldErrors = Object.values(currBaseMeta.errorMap ?? {}).filter(
  (val) => val !== undefined,
) as never

But the type definition allows undefined in the union, forcing consumers to add unnecessary undefined checks:

// Before: TypeScript thinks errors can contain undefined
errors.forEach(error => {
  console.log(error.length)  // TS error: Object is possibly 'undefined'
})

This wraps the error union types with NonNullable<> in both the FormState.errors type and the getAllErrors() return type, aligning the types with the runtime behavior.

// After: undefined is excluded from the type, matching runtime
errors.forEach(error => {
  console.log(error.length)  // Works
})

Fixes #2022

The runtime code filters out undefined values from the errorMap when
building the errors array (via .filter(val => val !== undefined)),
but the type definition allowed undefined in the union. This forced
consumers to add unnecessary runtime undefined checks when iterating
over errors.

Wrapping the error union with NonNullable aligns the type with the
actual runtime behavior.

Fixes TanStack#2022
@changeset-bot
Copy link

changeset-bot bot commented Mar 6, 2026

⚠️ No Changeset found

Latest commit: a8f268c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

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.

Bug Report: Type mismatch in errors field - runtime filtering of undefined not reflected in type definition

1 participant