title: tRPC How to encapsulate middleware?
body: <p>I am trying to put middleware into its own function but I am struggling to get proper typescript typing on it.</p>
  <p>At the moment I am typing the middleware as follows, but this isn't ideal because the type information of context and metadata are lost after returning from the middleware.</p>
  <p>Inside the middleware</p>
  <pre class="lang-js prettyprint-override"><code>import { MiddlewareFunction } from &quot;@trpc/server/dist/declarations/src/internals/middlewares&quot;;
  import { TRPCError } from &quot;@trpc/server&quot;;
  
  
  export const authMiddleware : MiddlewareFunction&lt;any, any, any&gt; = async ({ ctx, next, path, rawInput, type, meta }) =&gt; {
    if (!meta?.auth)
      return next();
  
    // some random logic
  
    return next();
  }
  </code></pre>
  <p>And this is how I want to consume it</p>
  <pre class="lang-js prettyprint-override"><code>createRouter()
    .middleware(authMiddleware)
    .mutation('', {
      meta: {
        auth: &quot;user&quot;,
        appCheck: true
      },
      input: object({
        workshopId: idSchema,
      }),
      resolve: async ({ input, ctx, type }) =&gt; {
      // Here ctx has been widened to any
      // ...
  </code></pre>
  <p>Thank you in advance.</p>