Impossible to programmatically set "onServer" errors with setErrorMap due to conflicting type definitions
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 35/100
- Issue 類型
- 缺陷
- 描述清晰度
- 基本清楚
- 活躍度
- 停滯
- 技術堆疊
- react, typescript
研究方向
先從 issue 中描述的 FormValidators、FormApi 和 BaseFormState 定義開始,然後使用 setErrorMap 和 useForm 在連結的 StackBlitz 範例中重現型別錯誤。當 onServer 能夠以型別安全的方式宣告並以程式設定,而不需要佔位 validator 或冗長的 workaround 時,工作即告完成。
由索引模型根據 Issue 內容生成。
描述
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
- 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!
- 主要語言
- TypeScript
- 星號
- 6.8k
- 分支
- 717
- 平均合併
- 5 天 22 小時
- 30 天內合併 PR
- 14
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
TanStack/form 的其他 Issue
-
難度 1/5 1 小時以內 新手友好度 88/100
維護者通常 1 天內回覆
-
Submit listener exceptions leave isSubmitting stuck true可能已有人在做 @henricop 於 15 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
handleSubmit calls onSubmit again while a submit is still pending (since 1.33.1)可能已有人在做 @twotnguyen 於 14 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
`isDefaultValue` is wrong when a field-level `defaultValue` is `null`可能已有人在做 @fenet-as 於 40 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆
-
Async validator doesn't re-trigger `isValidating` after first failed validation可能已有人在做 @drewsephski 於 44 天前認領。 未關閉
難度 2/5 1-2 天 新手友好度 72/100
維護者通常 1 天內回覆
相似的 Issue
-
clawsweeper:needs-maintainer-review clawsweeper:needs-product-decision clawsweeper:no-new-fix-pr clawsweeper:source-repro impact:ux-friction issue-rating: 🦞 diamond lobster P3
難度 2/5 1-3 小時 新手友好度 76/100
openclaw/openclaw#203385 · 1 則留言 · 1 個 reaction ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
nwverse/persona#12800 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 80/100
維護者通常 1 天內回覆
-
[MCP]: page snapshots in .playwright-mcp/ are not git-ignored (.playwright-cli/ is, since #42318)未關閉
難度 2/5 1-3 小時 新手友好度 62/100
microsoft/playwright#44289 ·
維護者通常 1 天內回覆
-
good first issue
難度 2/5 半天 新手友好度 65/100
維護者通常 1 天內回覆