Type Inference with T3 Stack

What is the best way to infer a type when using the t3 stack from an api call? React Query docs mention the use of Generics but i cant wrap my head around it. More specifically, i want to return an array of the Event Custom Datatype i have defined from the db but i get a type error Event[] is not assignable to type (type structure here). The weird thing data fetching works ok on the frontend. Any resources that would be useful looking into would be greatly appreciated. Thanks!
11 Replies
cje
cje3y ago
Inferring Types | tRPC
It is often useful to wrap functionality of your @trpc/client or @trpc/react-query api within other functions. For this purpose, it's necessary to be able to infer input types and output types generated by your @trpc/server router.
r.omaarr
r.omaarr3y ago
May i ask what is the reasoning behind defining the type again by using something like type Event = inferProcedureOutput<AppRouter["event"]["getAll"]>; when i have the type already defined in a schema file? I tried to disable pinging as i dont want to distrub you but i dont know if it worked sorry if it pinged you 🙏
cje
cje3y ago
the types that you infer from the router change automatically when the inputs/outputs of that router change so its impossible for them to be out of sync like lets say you change the prisma query to omit some fields now your schema doesnt match the type of what the db query returns but if you're inferring, it's still correct you shouldn't have to write schema for output types with trpc
r.omaarr
r.omaarr3y ago
should i still keep my schema for the event type in this case or should i make all components use the type from the query
cje
cje3y ago
where are you using this schema? since it has createdAt etc in it, i'm guessing only for output? then you probably don't need it
r.omaarr
r.omaarr3y ago
r.omaarr
r.omaarr3y ago
yeah its only for output
cje
cje3y ago
yea not necessary
r.omaarr
r.omaarr3y ago
in a reusable component something like this
cje
cje3y ago
just infer it way easier and less code
r.omaarr
r.omaarr3y ago
oh okay thanks for the help!!
Want results from more Discord servers?
Add your server