Impossible to programmatically set "onServer" errors with setErrorMap due to conflicting type definitions

Aperta
#1,643 5 commenti 7 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
react, typescript

Direzione di ricerca

Inizia con le definizioni di FormValidators, FormApi e BaseFormState descritte nell’issue, quindi riproduci gli errori di tipo nell’esempio StackBlitz collegato usando setErrorMap e useForm. Il lavoro è completato quando onServer può essere dichiarato e impostato programmaticamente in modo type-safe, senza validator segnaposto o workaround verbosi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

v1 v2: needs investigation
Describe the bug

Currently, it's impossible to programmatically set a form-level server error using the semantically correct form.setErrorMap({ onServer: '...' }) in a type-safe way without resorting to complex workarounds.

The goal is to use the onServer key, which seems designed for server-side validation errors that are not part of the standard validation lifecycle.

The type of errorMap.onServer is derived from the TOnServer generic of the useForm hook.

This TOnServer generic is inferred from a validators.onServer property when initializing the form.

However, the FormValidators type definition does not include an onServer property. Attempting to add it results in a TypeScript error: Object literal may only specify known properties, and 'onServer' does not exist in type 'FormValidators<...>'.

Because onServer cannot be declared in validators, the TOnServer generic is inferred as undefined.

This leads to the final error when trying to use setErrorMap: Type 'string[]' is not assignable to type 'undefined'.

This design makes the onServer error channel effectively unusable in a type-safe manner out-of-the-box, creating a frustrating developer experience for the very common use case of handling server errors from a data-fetching library like TanStack Query.

Your minimal, reproducible example

https://stackblitz.com/edit/tanstack-form-cxredtu7?file=src%2Findex.tsx

Steps to reproduce
  1. Go to the provided link
Expected behavior

The onServer key in setErrorMap should be a first-class, dedicated channel for programmatic errors, completely decoupled from the validators system.

Its type in the errorMap should default to unknown or any, allowing developers to easily set server-side errors from external sources like useMutation without needing placeholder validators or verbose explicit generics. This would align with the pattern seen in other popular libraries like react-hook-form's setError('root.serverError', ...) and would greatly improve the developer experience for server-validated forms.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

OS: macOS
Browser: Chrome
Browser version: Version 138.0.7204.169 (Official Build) (arm64)

TanStack Form adapter

react-form

TanStack Form version

v1.15.0

TypeScript version

No response

Additional context

I dig a bit and seems like the core issue seems to be that TOnServer in FormApi is constrained as a validator type (extends undefined | FormAsyncValidateOrFn<TFormData>).

A potential fix could be to change the core type definitions to treat onServer as a special case, decoupling it from this constraint. For example, modifying the BaseFormState's errorMap definition to give onServer a static, flexible type.

Sorry if I'm missing something!

Lingua principale
TypeScript
Stelle
6.8k
Fork
717
Merge medio
5g 22h
PR unite (30g)
14

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di TanStack/form

Tutte le issue di TanStack/form

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.