Basically I want to get the types of procedure keys and also have a guard function to check if passed in type is this particular router type or something else. Problem is that I can't export a server variable into a client variable like this.
This is what I have
// server component
export const routes = {
users: userRouter,
stores: storeRouter,
skus: skuRouter,
};
export type AppRoutes = typeof routes;
// Checking if procedure has any of the keys.
type HasDeleteProcedure<T> = T extends {
delete: unknown;
update: unknown;
create: unknown;
}
? true
: false;
export type TRPC_ROUTE_NAMESPACES = keyof typeof routes;
export type TRPC_ROUTE_NAMESPACES_MUTABLE = {
[K in TRPC_ROUTE_NAMESPACES]: HasDeleteProcedure<
(typeof routes)[K]
> extends true
? K
: never;
}[TRPC_ROUTE_NAMESPACES];
export const isTRPCRouteNamespacesMutable = <T extends typeof routes>(
value: any,
): value is keyof T => {
return (
typeof value === "string" &&
Object.keys(routes).some(
(key) =>
key === value &&
typeof routes[key as TRPC_ROUTE_NAMESPACES] === "object" &&
"delete" in routes[key as TRPC_ROUTE_NAMESPACES] &&
"update" in routes[key as TRPC_ROUTE_NAMESPACES] &&
"create" in routes[key as TRPC_ROUTE_NAMESPACES],
)
);
};
I used AI to help me with the types lol.