Skip to main content
Version: 10.x

Middlewares

You are able to add middleware(s) to a procedure with the t.procedure.use() method. The middleware(s) will wrap the invocation of the procedure and must pass through its return value.

Authorization

In the example below, any call to a adminProcedure will ensure that the user is an "admin" before executing.

ts
import { TRPCError, initTRPC } from '@trpc/server';
 
interface Context {
user?: {
id: string;
isAdmin: boolean;
// [..]
};
}
 
const t = initTRPC.context<Context>().create();
export const middleware = t.middleware;
export const publicProcedure = t.procedure;
export const router = t.router;
 
const isAdmin = middleware(async (opts) => {
const { ctx } = opts;
if (!ctx.user?.isAdmin) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
return opts.next({
ctx: {
user: ctx.user,
},
});
});
 
export const adminProcedure = publicProcedure.use(isAdmin);
ts
import { TRPCError, initTRPC } from '@trpc/server';
 
interface Context {
user?: {
id: string;
isAdmin: boolean;
// [..]
};
}
 
const t = initTRPC.context<Context>().create();
export const middleware = t.middleware;
export const publicProcedure = t.procedure;
export const router = t.router;
 
const isAdmin = middleware(async (opts) => {
const { ctx } = opts;
if (!ctx.user?.isAdmin) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
return opts.next({
ctx: {
user: ctx.user,
},
});
});
 
export const adminProcedure = publicProcedure.use(isAdmin);
ts
import { adminProcedure, publicProcedure, router } from './trpc';
 
const adminRouter = router({
secretPlace: adminProcedure.query(() => 'a key'),
});
 
export const appRouter = router({
foo: publicProcedure.query(() => 'bar'),
admin: adminRouter,
});
ts
import { adminProcedure, publicProcedure, router } from './trpc';
 
const adminRouter = router({
secretPlace: adminProcedure.query(() => 'a key'),
});
 
export const appRouter = router({
foo: publicProcedure.query(() => 'bar'),
admin: adminRouter,
});
tip

See Error Handling to learn more about the TRPCError thrown in the above example.

Logging

In the example below timings for queries are logged automatically.

ts
const loggerMiddleware = middleware(async (opts) => {
const start = Date.now();
 
const result = await opts.next();
 
const durationMs = Date.now() - start;
const meta = { path: opts.path, type: opts.type, durationMs };
 
result.ok
? console.log('OK request timing:', meta)
: console.error('Non-OK request timing', meta);
 
return result;
});
 
export const loggedProcedure = publicProcedure.use(loggerMiddleware);
ts
const loggerMiddleware = middleware(async (opts) => {
const start = Date.now();
 
const result = await opts.next();
 
const durationMs = Date.now() - start;
const meta = { path: opts.path, type: opts.type, durationMs };
 
result.ok
? console.log('OK request timing:', meta)
: console.error('Non-OK request timing', meta);
 
return result;
});
 
export const loggedProcedure = publicProcedure.use(loggerMiddleware);
ts
import { loggedProcedure, router } from './trpc';
 
export const appRouter = router({
foo: loggedProcedure.query(() => 'bar'),
abc: loggedProcedure.query(() => 'def'),
});
ts
import { loggedProcedure, router } from './trpc';
 
export const appRouter = router({
foo: loggedProcedure.query(() => 'bar'),
abc: loggedProcedure.query(() => 'def'),
});

Context Extension

"Context Extension" enables middlewares to dynamically add and override keys on a base procedure's context in a typesafe manner.

Below we have an example of a middleware that changes properties of a context, the changes are then available to all chained consumers, such as other middlewares and procedures:

ts
type Context = {
// user is nullable
user?: {
id: string;
};
};
 
const isAuthed = middleware((opts) => {
const { ctx } = opts;
// `ctx.user` is nullable
if (!ctx.user) {
(property) user?: { id: string; } | undefined
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
 
return opts.next({
ctx: {
// ✅ user value is known to be non-null now
user: ctx.user,
(property) user: { id: string; }
},
});
});
 
const protectedProcedure = publicProcedure.use(isAuthed);
protectedProcedure.query(({ ctx }) => ctx.user);
(property) user: { id: string; }
ts
type Context = {
// user is nullable
user?: {
id: string;
};
};
 
const isAuthed = middleware((opts) => {
const { ctx } = opts;
// `ctx.user` is nullable
if (!ctx.user) {
(property) user?: { id: string; } | undefined
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
 
return opts.next({
ctx: {
// ✅ user value is known to be non-null now
user: ctx.user,
(property) user: { id: string; }
},
});
});
 
const protectedProcedure = publicProcedure.use(isAuthed);
protectedProcedure.query(({ ctx }) => ctx.user);
(property) user: { id: string; }

Extending middlewares

info

We have prefixed this as unstable_ as it's a new API, but you're safe to use it! Read more.

We have a powerful feature called .pipe() which allows you to extend middlewares in a typesafe manner.

Below we have an example of a middleware that extends a base middleware(foo). Like the context extension example above, piping middlewares will change properties of the context, and procedures will receive the new context value.

ts
const fooMiddleware = middleware((opts) => {
return opts.next({
ctx: {
foo: 'foo' as const,
},
});
});
 
const barMiddleware = fooMiddleware.unstable_pipe((opts) => {
const { ctx } = opts;
ctx.foo;
(property) foo: "foo"
return opts.next({
ctx: {
bar: 'bar' as const,
},
});
});
 
const barProcedure = publicProcedure.use(barMiddleware);
barProcedure.query(({ ctx }) => ctx.bar);
(parameter) ctx: { foo: "foo"; bar: "bar"; }
ts
const fooMiddleware = middleware((opts) => {
return opts.next({
ctx: {
foo: 'foo' as const,
},
});
});
 
const barMiddleware = fooMiddleware.unstable_pipe((opts) => {
const { ctx } = opts;
ctx.foo;
(property) foo: "foo"
return opts.next({
ctx: {
bar: 'bar' as const,
},
});
});
 
const barProcedure = publicProcedure.use(barMiddleware);
barProcedure.query(({ ctx }) => ctx.bar);
(parameter) ctx: { foo: "foo"; bar: "bar"; }

Beware that the order in which you pipe your middlewares matter and that the context must overlap. An example of a forbidden pipe is shown below. Here, the fooMiddleware overrides the ctx.a while barMiddleware still expects the root context from the initialization in initTRPC - so piping fooMiddleware with barMiddleware would not work, while piping barMiddleware with fooMiddleware does work.

ts
import { initTRPC } from '@trpc/server';
 
const t = initTRPC
.context<{
a: {
b: 'a';
};
}>()
.create();
 
const fooMiddleware = t.middleware((opts) => {
const { ctx } = opts;
ctx.a; // 👈 fooMiddleware expects `ctx.a` to be an object
(property) a: { b: 'a'; }
return opts.next({
ctx: {
a: 'a' as const, // 👈 `ctx.a` is no longer an object
},
});
});
 
const barMiddleware = t.middleware((opts) => {
const { ctx } = opts;
ctx.a; // 👈 barMiddleware expects `ctx.a` to be an object
(property) a: { b: 'a'; }
return opts.next({
ctx: {
foo: 'foo' as const,
},
});
});
 
// ❌ `ctx.a` does not overlap from `fooMiddleware` to `barMiddleware`
fooMiddleware.unstable_pipe(barMiddleware);
Argument of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _meta: object; }, { ...; }>' is not assignable to parameter of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _meta: object; }, { ...; }>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends import("/vercel/path0/www/node_modules/@trpc/server/dist/core/procedure").ProcedureParams<import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: import("/vercel/path0/www/node_modules/@trpc/server/dis...' is not assignable to type '<$Params extends import("/vercel/path0/www/node_modules/@trpc/server/dist/core/procedure").ProcedureParams<import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: import("/vercel/path0/www/node_modules/@trpc/server/dis...'. Two different types with this name exist, but they are unrelated. Types of parameters 'fn' and 'fn' are incompatible. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...'. Two different types with this name exist, but they are unrelated. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...'. Two different types with this name exist, but they are unrelated. Types of property '_middlewares' are incompatible. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...'. Two different types with this name exist, but they are unrelated. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...'. Two different types with this name exist, but they are unrelated. Types of parameters 'opts' and 'opts' are incompatible. Type '{ ctx: { a: { b: "a"; }; foo: "foo"; }; type: "query" | "mutation" | "subscription"; path: string; input: unknown; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataT...' is not assignable to type '{ ctx: { a: "a"; foo: "foo"; }; type: "query" | "mutation" | "subscription"; path: string; input: unknown; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransform...'. The types of 'ctx.a' are incompatible between these types. Type '{ b: "a"; }' is not assignable to type '"a"'.2345Argument of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _meta: object; }, { ...; }>' is not assignable to parameter of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _meta: object; }, { ...; }>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends import("/vercel/path0/www/node_modules/@trpc/server/dist/core/procedure").ProcedureParams<import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: import("/vercel/path0/www/node_modules/@trpc/server/dis...' is not assignable to type '<$Params extends import("/vercel/path0/www/node_modules/@trpc/server/dist/core/procedure").ProcedureParams<import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: import("/vercel/path0/www/node_modules/@trpc/server/dis...'. Two different types with this name exist, but they are unrelated. Types of parameters 'fn' and 'fn' are incompatible. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...'. Two different types with this name exist, but they are unrelated. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...'. Two different types with this name exist, but they are unrelated. Types of property '_middlewares' are incompatible. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...'. Two different types with this name exist, but they are unrelated. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...'. Two different types with this name exist, but they are unrelated. Types of parameters 'opts' and 'opts' are incompatible. Type '{ ctx: { a: { b: "a"; }; foo: "foo"; }; type: "query" | "mutation" | "subscription"; path: string; input: unknown; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataT...' is not assignable to type '{ ctx: { a: "a"; foo: "foo"; }; type: "query" | "mutation" | "subscription"; path: string; input: unknown; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransform...'. The types of 'ctx.a' are incompatible between these types. Type '{ b: "a"; }' is not assignable to type '"a"'.
 
// ✅ `ctx.a` overlaps from `barMiddleware` and `fooMiddleware`
barMiddleware.unstable_pipe(fooMiddleware);
ts
import { initTRPC } from '@trpc/server';
 
const t = initTRPC
.context<{
a: {
b: 'a';
};
}>()
.create();
 
const fooMiddleware = t.middleware((opts) => {
const { ctx } = opts;
ctx.a; // 👈 fooMiddleware expects `ctx.a` to be an object
(property) a: { b: 'a'; }
return opts.next({
ctx: {
a: 'a' as const, // 👈 `ctx.a` is no longer an object
},
});
});
 
const barMiddleware = t.middleware((opts) => {
const { ctx } = opts;
ctx.a; // 👈 barMiddleware expects `ctx.a` to be an object
(property) a: { b: 'a'; }
return opts.next({
ctx: {
foo: 'foo' as const,
},
});
});
 
// ❌ `ctx.a` does not overlap from `fooMiddleware` to `barMiddleware`
fooMiddleware.unstable_pipe(barMiddleware);
Argument of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _meta: object; }, { ...; }>' is not assignable to parameter of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _meta: object; }, { ...; }>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends import("/vercel/path0/www/node_modules/@trpc/server/dist/core/procedure").ProcedureParams<import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: import("/vercel/path0/www/node_modules/@trpc/server/dis...' is not assignable to type '<$Params extends import("/vercel/path0/www/node_modules/@trpc/server/dist/core/procedure").ProcedureParams<import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: import("/vercel/path0/www/node_modules/@trpc/server/dis...'. Two different types with this name exist, but they are unrelated. Types of parameters 'fn' and 'fn' are incompatible. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...'. Two different types with this name exist, but they are unrelated. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...'. Two different types with this name exist, but they are unrelated. Types of property '_middlewares' are incompatible. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...'. Two different types with this name exist, but they are unrelated. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...'. Two different types with this name exist, but they are unrelated. Types of parameters 'opts' and 'opts' are incompatible. Type '{ ctx: { a: { b: "a"; }; foo: "foo"; }; type: "query" | "mutation" | "subscription"; path: string; input: unknown; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataT...' is not assignable to type '{ ctx: { a: "a"; foo: "foo"; }; type: "query" | "mutation" | "subscription"; path: string; input: unknown; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransform...'. The types of 'ctx.a' are incompatible between these types. Type '{ b: "a"; }' is not assignable to type '"a"'.2345Argument of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _meta: object; }, { ...; }>' is not assignable to parameter of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _meta: object; }, { ...; }>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends import("/vercel/path0/www/node_modules/@trpc/server/dist/core/procedure").ProcedureParams<import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: import("/vercel/path0/www/node_modules/@trpc/server/dis...' is not assignable to type '<$Params extends import("/vercel/path0/www/node_modules/@trpc/server/dist/core/procedure").ProcedureParams<import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: import("/vercel/path0/www/node_modules/@trpc/server/dis...'. Two different types with this name exist, but they are unrelated. Types of parameters 'fn' and 'fn' are incompatible. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...'. Two different types with this name exist, but they are unrelated. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/form...'. Two different types with this name exist, but they are unrelated. Types of property '_middlewares' are incompatible. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...'. Two different types with this name exist, but they are unrelated. Type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...' is not assignable to type 'import("/vercel/path0/www/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/vercel/path0/www/node_modules/@trpc/server/dist/core/internals/config").RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: import("/vercel/path0/www/node_modules/@trpc/server/dist/error/for...'. Two different types with this name exist, but they are unrelated. Types of parameters 'opts' and 'opts' are incompatible. Type '{ ctx: { a: { b: "a"; }; foo: "foo"; }; type: "query" | "mutation" | "subscription"; path: string; input: unknown; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataT...' is not assignable to type '{ ctx: { a: "a"; foo: "foo"; }; type: "query" | "mutation" | "subscription"; path: string; input: unknown; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { a: { b: "a"; }; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransform...'. The types of 'ctx.a' are incompatible between these types. Type '{ b: "a"; }' is not assignable to type '"a"'.
 
// ✅ `ctx.a` overlaps from `barMiddleware` and `fooMiddleware`
barMiddleware.unstable_pipe(fooMiddleware);

Experimental: standalone middlewares

info

Caution: we have prefixed this as experimental_ and it may change with any tRPC release. Read more.

tRPC has a new experimental API called experimental_standaloneMiddleware which allows you to independently define a middleware that can be used with any tRPC instance. Creating middlewares using t.middleware has the limitation that the Context type is tied to the Context type of the tRPC instance. This means that you cannot use the same middleware with multiple tRPC instances that have different Context types.

Using experimental_standaloneMiddleware you can create a middleware that explicitly defines its requirements, i.e. the Context, Input and Meta types:

ts
import {
experimental_standaloneMiddleware,
initTRPC,
TRPCError,
} from '@trpc/server';
import * as z from 'zod';
 
const projectAccessMiddleware = experimental_standaloneMiddleware<{
ctx: { allowedProjects: string[] }; // defaults to 'object' if not defined
input: { projectId: string }; // defaults to 'unknown' if not defined
// 'meta', not defined here, defaults to 'object | undefined'
}>().create((opts) => {
if (!opts.ctx.allowedProjects.includes(opts.input.projectId)) {
throw new TRPCError({
code: 'FORBIDDEN',
message: 'Not allowed',
});
}
 
return opts.next();
});
 
const t1 = initTRPC
.context<{
allowedProjects: string[];
}>()
.create();
 
// ✅ `ctx.allowedProjects` satisfies "string[]" and `input.projectId` satisfies "string"
const accessControlledProcedure = t1.procedure
.input(z.object({ projectId: z.string() }))
.use(projectAccessMiddleware);
 
// ❌ `ctx.allowedProjects` satisfies "string[]" but `input.projectId` does not satisfy "string"
const accessControlledProcedure2 = t1.procedure
.input(z.object({ projectId: z.number() }))
.use(projectAccessMiddleware);
Argument of type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to parameter of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; ... 5 more ...; _output_out: unknown; }, $Params> | MiddlewareFunction<....' is not assignable to type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, $Par...'. Types of parameters 'fn' and 'fn' are incompatible. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any> | MiddlewareFunction<...>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, $Params>...' is not assignable to type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; ... 5 more ...; _output_out: any; }, $Params> | MiddlewareFunction<...>)...'. Types of parameters 'fn' and 'fn' are incompatible. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any> | Middlew...' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any>'. Types of property '_middlewares' are incompatible. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>[]' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any>[]'. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any>'. Types of parameters 'opts' and 'opts' are incompatible. Type '{ ctx: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: number; }; rawInput: unknown; meta: object | undefined; next: { ...; }; }' is not assignable to type '{ ctx: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: string; }; rawInput: unknown; meta: object | undefined; next: { ...; }; }'. The types of 'input.projectId' are incompatible between these types. Type 'number' is not assignable to type 'string'.2345Argument of type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to parameter of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; ... 5 more ...; _output_out: unknown; }, $Params> | MiddlewareFunction<....' is not assignable to type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, $Par...'. Types of parameters 'fn' and 'fn' are incompatible. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any> | MiddlewareFunction<...>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, $Params>...' is not assignable to type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; ... 5 more ...; _output_out: any; }, $Params> | MiddlewareFunction<...>)...'. Types of parameters 'fn' and 'fn' are incompatible. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any> | Middlew...' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any>'. Types of property '_middlewares' are incompatible. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>[]' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any>[]'. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any>'. Types of parameters 'opts' and 'opts' are incompatible. Type '{ ctx: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: number; }; rawInput: unknown; meta: object | undefined; next: { ...; }; }' is not assignable to type '{ ctx: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: string; }; rawInput: unknown; meta: object | undefined; next: { ...; }; }'. The types of 'input.projectId' are incompatible between these types. Type 'number' is not assignable to type 'string'.
 
// ❌ `ctx.allowedProjects` does not satisfy "string[]" even though `input.projectId` satisfies "string"
const t2 = initTRPC
.context<{
allowedProjects: number[];
}>()
.create();
 
const accessControlledProcedure3 = t2.procedure
.input(z.object({ projectId: z.string() }))
.use(projectAccessMiddleware);
Argument of type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to parameter of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; ... 5 more ...; _output_out: unknown; }, $Params> | MiddlewareFunction<....' is not assignable to type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, $Par...'. Types of parameters 'fn' and 'fn' are incompatible. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any> | MiddlewareFunction<...>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any>'. The types of 'unstable_pipe(...)._middlewares' are incompatible between these types. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }>[]' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, { ...; }>[]'. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }>' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, { ...; }>'. Types of parameters 'opts' and 'opts' are incompatible. Type '{ ctx: { allowedProjects: string[]; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: string; }; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { ...; }; meta: object; errorShape: object; transformer: object; }>; ... 5 mo...' is not assignable to type '{ ctx: { allowedProjects: number[]; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: string; }; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { ...; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDa...'. The types of 'ctx.allowedProjects' are incompatible between these types. Type 'string[]' is not assignable to type 'number[]'.2345Argument of type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to parameter of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; ... 5 more ...; _output_out: unknown; }, $Params> | MiddlewareFunction<....' is not assignable to type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, $Par...'. Types of parameters 'fn' and 'fn' are incompatible. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any> | MiddlewareFunction<...>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any>'. The types of 'unstable_pipe(...)._middlewares' are incompatible between these types. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }>[]' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, { ...; }>[]'. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }>' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, { ...; }>'. Types of parameters 'opts' and 'opts' are incompatible. Type '{ ctx: { allowedProjects: string[]; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: string; }; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { ...; }; meta: object; errorShape: object; transformer: object; }>; ... 5 mo...' is not assignable to type '{ ctx: { allowedProjects: number[]; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: string; }; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { ...; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDa...'. The types of 'ctx.allowedProjects' are incompatible between these types. Type 'string[]' is not assignable to type 'number[]'.
ts
import {
experimental_standaloneMiddleware,
initTRPC,
TRPCError,
} from '@trpc/server';
import * as z from 'zod';
 
const projectAccessMiddleware = experimental_standaloneMiddleware<{
ctx: { allowedProjects: string[] }; // defaults to 'object' if not defined
input: { projectId: string }; // defaults to 'unknown' if not defined
// 'meta', not defined here, defaults to 'object | undefined'
}>().create((opts) => {
if (!opts.ctx.allowedProjects.includes(opts.input.projectId)) {
throw new TRPCError({
code: 'FORBIDDEN',
message: 'Not allowed',
});
}
 
return opts.next();
});
 
const t1 = initTRPC
.context<{
allowedProjects: string[];
}>()
.create();
 
// ✅ `ctx.allowedProjects` satisfies "string[]" and `input.projectId` satisfies "string"
const accessControlledProcedure = t1.procedure
.input(z.object({ projectId: z.string() }))
.use(projectAccessMiddleware);
 
// ❌ `ctx.allowedProjects` satisfies "string[]" but `input.projectId` does not satisfy "string"
const accessControlledProcedure2 = t1.procedure
.input(z.object({ projectId: z.number() }))
.use(projectAccessMiddleware);
Argument of type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to parameter of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; ... 5 more ...; _output_out: unknown; }, $Params> | MiddlewareFunction<....' is not assignable to type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, $Par...'. Types of parameters 'fn' and 'fn' are incompatible. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any> | MiddlewareFunction<...>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, $Params>...' is not assignable to type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; ... 5 more ...; _output_out: any; }, $Params> | MiddlewareFunction<...>)...'. Types of parameters 'fn' and 'fn' are incompatible. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any> | Middlew...' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any>'. Types of property '_middlewares' are incompatible. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>[]' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any>[]'. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any>'. Types of parameters 'opts' and 'opts' are incompatible. Type '{ ctx: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: number; }; rawInput: unknown; meta: object | undefined; next: { ...; }; }' is not assignable to type '{ ctx: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: string; }; rawInput: unknown; meta: object | undefined; next: { ...; }; }'. The types of 'input.projectId' are incompatible between these types. Type 'number' is not assignable to type 'string'.2345Argument of type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to parameter of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; ... 5 more ...; _output_out: unknown; }, $Params> | MiddlewareFunction<....' is not assignable to type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, $Par...'. Types of parameters 'fn' and 'fn' are incompatible. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any> | MiddlewareFunction<...>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, $Params>...' is not assignable to type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; ... 5 more ...; _output_out: any; }, $Params> | MiddlewareFunction<...>)...'. Types of parameters 'fn' and 'fn' are incompatible. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any> | Middlew...' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any>'. Types of property '_middlewares' are incompatible. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>[]' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any>[]'. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; _input_in: { ...; }; _input_out: { ...; }; _output_in: any; _output_out: any; }, any>' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: any; }, any>'. Types of parameters 'opts' and 'opts' are incompatible. Type '{ ctx: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: number; }; rawInput: unknown; meta: object | undefined; next: { ...; }; }' is not assignable to type '{ ctx: { [x: string]: any; [x: number]: any; [x: symbol]: any; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: string; }; rawInput: unknown; meta: object | undefined; next: { ...; }; }'. The types of 'input.projectId' are incompatible between these types. Type 'number' is not assignable to type 'string'.
 
// ❌ `ctx.allowedProjects` does not satisfy "string[]" even though `input.projectId` satisfies "string"
const t2 = initTRPC
.context<{
allowedProjects: number[];
}>()
.create();
 
const accessControlledProcedure3 = t2.procedure
.input(z.object({ projectId: z.string() }))
.use(projectAccessMiddleware);
Argument of type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to parameter of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; ... 5 more ...; _output_out: unknown; }, $Params> | MiddlewareFunction<....' is not assignable to type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, $Par...'. Types of parameters 'fn' and 'fn' are incompatible. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any> | MiddlewareFunction<...>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any>'. The types of 'unstable_pipe(...)._middlewares' are incompatible between these types. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }>[]' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, { ...; }>[]'. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }>' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, { ...; }>'. Types of parameters 'opts' and 'opts' are incompatible. Type '{ ctx: { allowedProjects: string[]; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: string; }; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { ...; }; meta: object; errorShape: object; transformer: object; }>; ... 5 mo...' is not assignable to type '{ ctx: { allowedProjects: number[]; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: string; }; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { ...; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDa...'. The types of 'ctx.allowedProjects' are incompatible between these types. Type 'string[]' is not assignable to type 'number[]'.2345Argument of type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to parameter of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<deriveParamsFromConfig<RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>, { projectId: string; }>, deriveParamsFromConfig<...>>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unique symbol; }, deriveParamsFromConfig<...>>'. Types of property 'unstable_pipe' are incompatible. Type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; ... 5 more ...; _output_out: unknown; }, $Params> | MiddlewareFunction<....' is not assignable to type '<$Params extends ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>(fn: MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, $Par...'. Types of parameters 'fn' and 'fn' are incompatible. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any> | MiddlewareFunction<...>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any> | MiddlewareFunction<...>'. Type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, any>' is not assignable to type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, any>'. The types of 'unstable_pipe(...)._middlewares' are incompatible between these types. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }>[]' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, { ...; }>[]'. Type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: number[]; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDataTransformer; }>; ... 5 more ...; _output_out: unknown; }, { ...; }>' is not assignable to type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { allowedProjects: string[]; }; meta: object; errorShape: object; transformer: object; }>; _meta: object; _ctx_out: {}; _input_in: { projectId: string; }; _input_out: { ...; }; _output_in: unknown; _output_out: unknown; }, { ...; }>'. Types of parameters 'opts' and 'opts' are incompatible. Type '{ ctx: { allowedProjects: string[]; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: string; }; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { ...; }; meta: object; errorShape: object; transformer: object; }>; ... 5 mo...' is not assignable to type '{ ctx: { allowedProjects: number[]; }; type: "query" | "mutation" | "subscription"; path: string; input: { projectId: string; }; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { ...; }; meta: object; errorShape: DefaultErrorShape; transformer: DefaultDa...'. The types of 'ctx.allowedProjects' are incompatible between these types. Type 'string[]' is not assignable to type 'number[]'.