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
cornflour
cornflour2y ago
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]
cje
cje2y ago
use the RouterOutputs type from your api.ts
균어
균어2y ago
getting error typescript: Type '(ServiceOrder & { vehicle: Vehicle & { model: { name: string; }; }; })[] | undefined' has no matching index signature for type 'number'. [2537]
Keef
Keef2y ago
@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
Keef
Keef2y ago
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.
Want results from more Discord servers?
Add your server