isitayushI
tRPC3y 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"] } },
};
It works however, I get a red squiggly (error) under the example in my permissions object with the following error message.
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. : )
Was this page helpful?