Replies: 1 comment
|
There's no Both things you noticed are real. On 1.33.2:
type IsAny<T> = 0 extends 1 & T ? true : false
function useB(field: AnyFieldApi) {
const v = field.state.value
const anyCheck: true = null as unknown as IsAny<typeof v> // compiles -> v is any
}So it's not that you're missing a utility type; the escape hatch really does cost you what you think it costs. The supported route for a reusable sub-component is import { createFormHook, createFormHookContexts } from '@tanstack/react-form'
const { fieldContext, formContext } = createFormHookContexts()
const { withFieldGroup } = createFormHook({
fieldContext,
formContext,
fieldComponents: {},
formComponents: {},
})
export const TreeNodeRows = withFieldGroup({
defaultValues: [] as MultiPriceItem[],
props: { deep: 0 },
render: function Render({ group, deep }) {
const value = group.state.values // MultiPriceItem[]
const name = value[0]?.name // string | undefined
// ...
},
})
I put the same probe through const typedCheck: MultiPriceItem[] = value // assignable
const notAny: false = null as unknown as IsAny<typeof value> // holds -> not any
const firstName: string | undefined = value[0]?.name
// @ts-expect-error -- and typos are still caught
const bad = value[0]?.nopeAll of it type-checks clean, including the The parent then mounts it against the matching part of the form rather than handing over a field: <TreeNodeRows form={form} fields="items" deep={0} />Which is the reason there's no One caveat: if |
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone!I am building a reusable component that needs to accept a form field instance as a prop. My field value is strictly typed using a Zod schema (MultiPriceItem[]), but when I try to pass it to FieldApi, TypeScript throws an error because the class requires 23 explicit generic type arguments.Here is a simplified look at my Zod schema and my component's props interface:
What I've tried:
My Question:
What is the recommended, idiomatic way in TanStack Form to strongly type the TData (value) of a FieldApi instance inside reusable sub-components without manually providing all 23 generic parameters? Is there a built-in utility type (like an InferFieldApi or similar) that I missed in the docs?
Thank you in advance! 🙌
All reactions