T
TanStack8mo ago
absent-sapphire

createServerFn with generic validators and return types

I'm using Start, and there are lots of 'boilerplate'-y code I would need to write for individual types. I created some builder functions to reduce this boilerplate, but I can't seem to get past this strange TS error in the handler()
Types of property 'records' are incompatible.
Type 'T[]' is not assignable to type 'TransformerStringify<T, DefaultSerializable>[]'.
Type 'T' is not assignable to type 'TransformerStringify<T, DefaultSerializable>'.
Types of property 'records' are incompatible.
Type 'T[]' is not assignable to type 'TransformerStringify<T, DefaultSerializable>[]'.
Type 'T' is not assignable to type 'TransformerStringify<T, DefaultSerializable>'.
Here's a small reproduction of the issue attached. If anyone knows what the issue could be / how to fix it, I would really appreciate a few pointers :))
5 Replies
genetic-orange
genetic-orange8mo ago
does this work at runtime?
absent-sapphire
absent-sapphireOP8mo ago
Yes, both when I create the serverFns per individual type (in that case perfectly fine, no TS errors), and when I create it via this generic wrapper, except for the TS error. Seems like I may just need to specify some type spec in the validator, but I haven't had the time to dive deeper into the actual library code to see what I may need.
genetic-orange
genetic-orange8mo ago
cc @Chris Horobin
generous-apricot
generous-apricot8mo ago
huh, i'm surprised that works - createServerFn is pretty picky about being assigned to a variable
absent-sapphire
absent-sapphireOP8mo ago
I seem to have had a weird combination of these issues all at once, tweaking it slightly finally gave me a reasonable error related to how createServerFn can only be called as a top-level function and can't be nested in some type of factory/builder. Ended up going back to the original approach of just having them defined individually per-type. Seems like a neat improvement to add this in a future release, but I suspect due to the way the endpoint generation in handled internally, it may not be possible, hence why it's explicitly locked down to only top-level instantiation?

Did you find this page helpful?