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

Aberta
#1,643 5 comentários 7 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
35/100
Tipo de issue
Bug
Clareza
Razoavelmente clara
Status de atividade
Estagnada
Stack de tecnologia
react, typescript

Direção de pesquisa

Comece pelas definições de FormValidators, FormApi e BaseFormState descritas na issue e, em seguida, reproduza os erros de tipo no exemplo vinculado do StackBlitz usando setErrorMap e useForm. O trabalho estará concluído quando onServer puder ser declarado e definido programaticamente de forma type-safe, sem validadores placeholder ou workarounds verbosos.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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!

Linguagem predominante
TypeScript
Estrelas
6.8k
Forks
717
Merge médio
5d 22h
PRs com merge (30d)
14

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de TanStack/form

Todas as issues de TanStack/form

Issues semelhantes

Mais issues de TypeScript

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.