Skip to content
Source
Sign inSign up
TanStack/form

Error types are invalid when using globalFormValidation syntax #1437

OpenV1OL3TF0X(GitHub profile, opens in a new tab) opened this issue 4 comments

Describe the bug

When returning an error from form validator in the form of { form: {}, fields: {} } it gets normalized, meaning the value in state.errorMap or state.errors is equal to error.form. This however is not reflected in type inference. Additionally, error is only normalized if it has fields field - returning { form: {} } is not normalized.

Your minimal, reproducible example

https://stackblitz.com/edit/vitejs-vite-dh4zqbne?file=src%2FApp.tsx

Steps to reproduce

  1. Open the example
  2. Input a value shorter than 10 characters into the input field
  3. hover over state.errors and state.errorMap types

Additonally, for weird mapping behaviour - switch the checkbox to disable the fields field in errors and see the value return t obeing an object.

Expected behavior

As a user, I'd expect the type to be correctly inferred and narrowed when the error passes isGlobalFormValidatonError check (in the example, the type should be string | undefined)

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: macos, Sequoia 15.41
  • Browser: Zen
  • browser version: 1.11.5b

TanStack Form adapter

react-form

TanStack Form version

v1.6.3

TypeScript version

v5.7.2

Additional context

No response

  1. LeCarbonator(GitHub profile, opens in a new tab) added labelsv2: needs investigation

  2. LeCarbonator(GitHub profile, opens in a new tab) removed labelsv2: needs investigation

  3. LeCarbonator(GitHub profile, opens in a new tab) added labelstype: bug

  4. LeCarbonator(GitHub profile, opens in a new tab) added labelsscope: core

  5. LeCarbonator(GitHub profile, opens in a new tab) added labelsarea: types

  6. Fixed in v2, but still present as of v1.33.5. The error type changed between the issue's reported version and latest, but the error is still wrong.

  7. @LeCarbonator My case is a form-level Standard Schema validator:

    validators: { onDynamic: quotePetSchema } // zod

    field.state.meta.errorMap.onDynamic infers as undefined, and field.state.meta.errors doesn't narrow to StandardSchemaV1Issue either — so there's no typed way to read .message without a cast or falling back to AnyFieldApi. This looks closely related to #1172, where field errors infer as undefined[] when only a form-level schema is present.

    I'll be honest that this is a little deflating for a library that leads with type safety. A single Zod schema at the form level is about the most common setup there is, and it's the one place the inference doesn't hold. The Custom Errors guide states that each errorMap key has exactly the type returned by its corresponding validator, which isn't what happens on this path.

    Is a fix planned for the v1 line?

  8. @CodeAndParty please provide a minimal repro and we'll see if we can look into it. Seems fairly different from what's reported here

  9. @crutchcorn I forogt to answer you on this thread, sorry.

    Please take a look at the PR I opened

Sign in to join this conversation.