how to get proper type when using form.pushFieldValue ?
Hello everyone π
First of all, thank you so much for this amazing library !! π
I've been playing with mode="array" and pushing values
Works flawlessly when accessing the array field via <form.AppField>
However trying to do so imperatively via form.pushFieldValue, despite working properly, shows a type error as pushFieldValue's first argument "field" is somehow typed as "never" (despite everything else being properly typed)
form.pushFieldValue is not mentioned in the docs so i'm not sure how to use it properly, any help would be much appreciated π


15 Replies
fair-roseOPβ’2mo ago
No issue at all when accessing like so

fascinating-indigoβ’2mo ago
that means it could not find a field that you have which is an array
what are your defaultValues?
fair-roseOPβ’2mo ago


fascinating-indigoβ’2mo ago
one issue that it currently has is that it cannot work with optional / nullable arrays
oh, I guess that solves it then. We have an issue requesting the type to be widened, Iβll see if I can link it
fair-roseOPβ’2mo ago
thanks ! π
let me try with a non optionnal array to confirm
fair-roseOPβ’2mo ago
indeed, this was the issue

fascinating-indigoβ’2mo ago
Wow, this issue is old β¦ I have some time today, and I already know what needs to be done to implement this. After some CRs, Iβll try to take care of it.
Issue: https://github.com/TanStack/form/issues/1588
GitHub
Allow optional arrays to have array methods Β· Issue #1588 Β· TanSt...
Much of my data is optional as I'm building a content creation form for TTRPG systems. This is a fairly complex form and there's a lot of variety even within a group of what's actually ...
fair-roseOPβ’2mo ago
this is amazing ! such a fast response time, problem identified in seconds, fix already on it's way π
thank you so much for your reactivity !
fascinating-indigoβ’2mo ago
no worries!
What's the behaviour you would have expected if you called
pushFieldValue on null at runtime? An error informing you about there not being an array? Silently ignoring the method?fair-roseOPβ’2mo ago
haven't thought about it but I would assume it would actually use an empty array and push the value onto that
if form default value is [] | null and we pushFieldValue I think I would expect to have an array with the pushed value
default is null -> we add an extra item so an array is created
fascinating-indigoβ’2mo ago
that would cover push only, but you have replace, swap, insert, remove and clear
fair-roseOPβ’2mo ago
true, I lack overview to provide a good spec sorry π
how does it currently work when accessing the field via <form.AppField> currently ? because that works flawlessly with null default so perhaps the behaviour could be the same
from my testing the existing behaviour of form.pushFieldValue was actually working, despite having a null default
i only made this ticket because the first argument, field, was typed as never, but it was working
fascinating-indigoβ’2mo ago
no worries, thereβs a plan. Iβll let you know once itβs out
fascinating-indigoβ’2mo ago
https://github.com/TanStack/form/issues/1823 there we go. Feedback is welcome @Alexis.dev
GitHub
[RFC] form-core: Handling Array Methods Called on
null or `undefi...RFC (form-core): Handling Array Methods Called on null or undefined Background TanStack Form provides several helper methods for working with array fields. In version form-core@1.24.4, these includ...
fair-roseOPβ’2mo ago
very clean document thank you so much !
i was trying to help a colleague with this demo, I sent him the link and asked to contribute by providing his feedbacks
hope this will help