Cannot access 't' before initialization
useAuthenticaton.ts
import { TRPCError } from '@trpc/server' import { t, User } from '../server/api' import * as Auth from '../services/auth/auth' const useAuthentication = t.middleware(async ({ ctx, next }) => { try { const authToken = ctx.req.headers['token'] as string const authResult = (await Auth.verifyJWTToken(authToken)) as User return next({ ctx: { user: authResult } }) } catch (error) { throw new TRPCError({ code: 'UNAUTHORIZED' }) } }) export default useAuthentication
api.ts
export type User = JwtPayload & { userid: number email: string } export type Context = inferAsyncReturnType<typeof createContext> export type ServerContext = { user: User } & Context export const t = initTRPC .context<ServerContext>() .meta<OpenApiMeta>() .create({ transformer: superjson, errorFormatter: ({ error, shape }) => { if ( error.code === 'INTERNAL_SERVER_ERROR' && process.env.NODE_ENV === 'production' ) { return { ...shape, message: 'Internal server error' } } return shape } })