Union types are not handled correctly
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 6.7k
- Forks
- 682
- Avg merge
- 5d 18h
- Merged PRs (30d)
- 7
Description
Describe the bug
The internal types of Tanstack Form, DeepValue and DeepKeysAndValues don't handle union types correctly and seem to collapse them.
type Example = { id: string } | { id: undefined };
type Test = DeepKeysAndValues<Example>
// ^? { key: "id"; value: string; }
const testObj: { id: string } | { id: undefined } = Math.random()
? { id: undefined }
: { id: '' }
type idField = DeepValue<typeof testObj, 'id'>
// Type error - TS2322: Type undefined is not assignable to type string
const a: idField = undefined
const b: idField = 'zzz'
My use case was using a union type with withForm, this also happens with withFieldGroup
import type { MergeExclusive, RequiredDeep } from 'type-fest'
export type EditType = ContactType & ContactPatchType // ContactType has readonly properties like `id`, ContactPatchType has the properties that are editable
export type CreateType = RequiredDeep<ContactDataType> // ContactDataType contains properties that are used for initial create, so no `id` for example
type EitherType = MergeExclusive<CreateType, EditType>
export const ContactFormFields = withForm({
defaultValues: {} as EitherType,
...
EditType contains {id: string} while CreateType does not. You would expect a form to understand that id may be string | undefined
Instead it becomes just {id: string}
Due to the incorrect type, this does not produce a type error but can fail in runtime since field.state.value may be undefined
<group.AppField name="id">
{(field) => <div>ID: {field.state.value.length}</div>}
</group.AppField>
Your minimal, reproducible example
N/A
Steps to reproduce
^ See Above
Expected behavior
Union types to be handled properly
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
"@tanstack/react-form": "1.23.7",
TanStack Form adapter
react-form
TanStack Form version
1.23.7
TypeScript version
5.9.3
Additional context
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Locate the internal DeepValue and DeepKeysAndValues definitions, then reproduce the issue with the union examples in the report and trace how their keys and values are inferred. Done means the resulting value type preserves undefined for the union and the withForm and withFieldGroup use cases no longer accept an incorrectly narrowed type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100