I have written the following in typescript for a dynamic form
type FormData = {
name: string,
session: { name: string }[]
}
...
const { control, register, handleSubmit } = useForm<FormData>()
const { fields, insert, remove, move, append } = useFieldArray({
control,
name: "session",
keyName: "id"
})
...
{
fields.map((field, index) => {
return <div key={field.id}>
<div>
<label>Session {index}</label>
<input type={"text"} {...register(`session.${index}.name`)} />
</div>
</div>
})
}
and I get the error at the part {...register(session.${index}.name)}
Argument of type 'string' is not assignable to parameter of type '"session" | "name" |
session.${number}|session.${number}.name' ts(2345)
The error goes away when i dont pass in FormData
const { control, register, handleSubmit } = useForm()