How to get singular type on trpc query
const serviceOrders = api.serviceOrders.getAll.useQuery({});
const [selectedServiceOrder, setSelectedServiceOrder] =
useState<(typeof serviceOrders.data)[0]>();
it says
typescript: Property '0' does not exist on type '(ServiceOrder & { vehicle: Vehicle & { model: { name: string; }; }; })[] | undefined'. [2339]
5 Replies
try
(typeof serviceOrders.data)[number]
, i think that should work
from my understanding, your type there is an array, so to get the type of an item in the array its ArrayType[number]
use the
RouterOutputs
type from your api.ts
getting error
typescript: Type '(ServiceOrder & { vehicle: Vehicle & { model: { name: string; }; }; })[] | undefined' has no matching index signature for type 'number'. [2537]
@amroos988 this
You would do RouterOutputs[path][to][query] which gets you the full return
You might be able to do [arrayProperty][number] on the previous type to get the type of one element in your array
Inferring Types | tRPC
In addition to the type inference made available by @trpc/server (see here) this integration also provides some inference helpers for usage purely in React.