Replies: 2 comments 1 reply
-
I guess you miss import React from 'react';
import { useFieldArray, UseFieldArrayReturn, FieldArrayPath } from 'react-hook-form'
type List = { list: {
id: string
name: string
}[] }
type WithGenericsType<T extends List> = UseFieldArrayReturn<
T,
'list' & FieldArrayPath<T>
>
type GenericUseFieldArrayReturn1 = WithGenericsType<{
groupName: 'group-1',
list: {
id: string,
name: string
}[]
}>
// With intersection
type GenericUseFieldArrayReturn2 = WithGenericsType<{
groupName: 'group-2',
} & List>
// Type checking
const method: GenericUseFieldArrayReturn2 = useFieldArray({ name: 'list' }) |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks @sujinleeme. Your idea to use I had a discussion with my colleague about your solution and experimented a bit. I found an alternative that might be more precise: type WithGenericsType<T extends ListType> = UseFieldArrayReturn<
T,
T['list'], // Using the type of 'list' directly from T
'fieldKey'
>; This keeps the type strictly tied to Thought this might be interesting for you as well. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I've encountered a TypeScript error when trying to use
UseFieldArrayReturn
with a generic type parameter. The issue does not occur when using a non-generic type. Here's a brief overview:The error specifically states: Type '"list"' does not satisfy the constraint 'ArrayPath'.
I followed the TypeScript integration guide from the official documentation (https://react-hook-form.com/ts#UseFieldArrayReturn) and expected the generic version to work similarly to the non-generic version without errors.
Could you please provide insight or a solution for this issue? Is there an alternative approach to using
UseFieldArrayReturn
with generic type parameters, or is this an oversight in the type definitions?Here's a reproduction of the issue: https://stackblitz.com/edit/react-hook-form-8heqxa?file=src%2FApp.tsx&terminal=dev
Beta Was this translation helpful? Give feedback.
All reactions