T
TanStackβ€’2mo ago
fair-rose

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 πŸ™‚
No description
No description
15 Replies
fair-rose
fair-roseOPβ€’2mo ago
No issue at all when accessing like so
No description
fascinating-indigo
fascinating-indigoβ€’2mo ago
that means it could not find a field that you have which is an array what are your defaultValues?
fair-rose
fair-roseOPβ€’2mo ago
No description
No description
fascinating-indigo
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-rose
fair-roseOPβ€’2mo ago
thanks ! πŸ™‚ let me try with a non optionnal array to confirm
fair-rose
fair-roseOPβ€’2mo ago
indeed, this was the issue
No description
fascinating-indigo
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&#39;m building a content creation form for TTRPG systems. This is a fairly complex form and there&#39;s a lot of variety even within a group of what&#39;s actually ...
fair-rose
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
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-rose
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
fascinating-indigoβ€’2mo ago
that would cover push only, but you have replace, swap, insert, remove and clear
fair-rose
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
fascinating-indigoβ€’2mo ago
no worries, thereβ€˜s a plan. Iβ€˜ll let you know once itβ€˜s out
fascinating-indigo
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-rose
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

Did you find this page helpful?