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

Abierto
#1,643 5 comentarios 7 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
react, typescript

Línea de trabajo

Comienza con las definiciones de FormValidators, FormApi y BaseFormState descritas en el issue y, a continuación, reproduce los errores de tipos en el ejemplo de StackBlitz enlazado usando setErrorMap y useForm. El trabajo estará terminado cuando onServer pueda declararse y establecerse mediante programación de forma segura respecto a los tipos, sin validadores de marcador de posición ni soluciones alternativas verbosas.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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!

Lenguaje dominante
TypeScript
Estrellas
6.8k
Forks
717
Merge medio
5 d 22 h
PR fusionados (30 d)
14

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de TanStack/form

Todos los issues de TanStack/form

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.