Is it possible to pass down a generic to my TRPC API route (procedure)?

From my front end, I need to pass a generic down to my TRPC procedure. Is this possible? The reason is because the fn that runs within the procedure will change the return types based on the generic it receives.
6 Replies
Brendonovich
Brendonovich2y ago
Nah you can’t have generic procedures, that’s a bit out of typescript’s ability
DYELbrah
DYELbrahOP2y ago
Dang, so lets say I want to call this function that's usable to fetch data from multiple tables such as:
const getTableData = async <T>({
tableName,
columns,
}: SearchInput<T>) => {
const { data } = await supabase
.from(tableName)
.select(columns.join(","));
return data;
};
const getTableData = async <T>({
tableName,
columns,
}: SearchInput<T>) => {
const { data } = await supabase
.from(tableName)
.select(columns.join(","));
return data;
};
Will I basically have to break it down into an individual function for each table? Probably a router/procedure for each individual table/set I need?
Brendonovich
Brendonovich2y ago
You’ll need multiple procedures but can reuse that function in them
DYELbrah
DYELbrahOP2y ago
Thanks! I'm not too sure what you mean, I'm thinking I'll have to just hardcode the table name/columns? So instead of having a customer search any table endpoint, I'll have an endpoint (procedure) for each one. Especially if I don't want to break the typesafety Without passing the generics, the types returned from my calls are set to any[] sadly
Brendonovich
Brendonovich2y ago
You’ll need to provide the generics each time you use it, since the retuned types will be different everytime
DYELbrah
DYELbrahOP2y ago
Ohhh nice that's a good point I can hardcode the generic
Want results from more Discord servers?
Add your server