TanStack / TanStack/form

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

Open
#1,643 5 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

v1 v2: needs investigation
Dominant language
TypeScript
Stars
6.7k
Forks
682
Avg merge
5d 18h
Merged PRs (30d)
7

Description

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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the FormValidators, FormApi, and BaseFormState definitions described in the issue, then reproduce the type errors in the linked StackBlitz example using setErrorMap and useForm. The work is done when onServer can be declared and set programmatically in a type-safe way without placeholder validators or verbose workarounds.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
developer-experience, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.