Vinnie
Vinnie
TtRPC
Created by Vinnie on 5/12/2024 in #❓-help
Infer context from procedure after middleware
I'm using trpc version 10 on pnpm. I I have my handler functions in separate files from the routers themselves. I'm looking to type the context correctly based on the procedure the handler is used with. Currently I am setting the ctx type from the Context type that is inferred from the createContext function.
export type GetPreferencesHandlerOptions = {
ctx: Context;
};

export const getPreferencesHandler = async ({
ctx,
}: GetPreferencesHandlerOptions) => {
const { viewer } = ctx;
// /\
// ||
// Viewer | undefined

// ... rest of code
}
export type GetPreferencesHandlerOptions = {
ctx: Context;
};

export const getPreferencesHandler = async ({
ctx,
}: GetPreferencesHandlerOptions) => {
const { viewer } = ctx;
// /\
// ||
// Viewer | undefined

// ... rest of code
}
The context type should rather be inferred from the privateProcedure so the changes which are made to the context in middlewares will also be correctly typed. I did see the inferProcedureBuilderResolverOptions helper type, but it is only available in version 11, and version 11 hasn't been released yet. Is there a way to do this in v10? https://trpc.io/docs/server/procedures#inferProcedureBuilderResolverOptions
4 replies