tRPCttRPC
Powered by
isitayushI
tRPC•3y ago•
27 replies
isitayush

typesafe permissions

Hi, So I wanted to infer all the procedures from my router recursively & assign a permission (string[]) to each one them. I wrote the following,
type GetProceduresRecusivelyAndAssignPermissions<T extends AnyRouter> = {
  [K in keyof T]: T[K] extends AnyProcedure
    ? { permissions: string[] }
    : T[K] extends AnyRouter
    ? GetProceduresRecusivelyAndAssignPermissions<T[K]>
    : never;
};

const permissions: GetProceduresRecusivelyAndAssignPermissions<
  typeof appRouter
> = {
  example: { getData: { permissions: ["canGetData"] } },
};
type GetProceduresRecusivelyAndAssignPermissions<T extends AnyRouter> = {
  [K in keyof T]: T[K] extends AnyProcedure
    ? { permissions: string[] }
    : T[K] extends AnyRouter
    ? GetProceduresRecusivelyAndAssignPermissions<T[K]>
    : never;
};

const permissions: GetProceduresRecusivelyAndAssignPermissions<
  typeof appRouter
> = {
  example: { getData: { permissions: ["canGetData"] } },
};
It works however, I get a red squiggly (error) under the
example
example
in my
permissions
permissions
object with the following error message.
Type '{ getData: { permissions: string[]; }; }' is missing the following properties from type 'GetProceduresRecusivelyAndAssignPermissions<CreateRouterInner<RootConfig<{ ctx: ....
Type '{ getData: { permissions: string[]; }; }' is missing the following properties from type 'GetProceduresRecusivelyAndAssignPermissions<CreateRouterInner<RootConfig<{ ctx: ....

In my above type, I am trying to check if the key is a procedure, then simply returning the permissions. Otherwise checking if the key is a router then repeating it recursively. I'm unable to figure out what I'm doing wrong. Can someone help fix this. : )
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Validating Permissions
RhysRRhys / ❓-help
4y ago
Distribute typesafe tRPC Client in an NPM library
TimTTim / ❓-help
3y ago
Where should authorization/permissions checks happen?
.jsonp..jsonp / ❓-help
8mo ago