You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using trpc in bunch of projects that are spread across npm packages. E.g., I have a server my-server and a client my-client. In my-client code I am importing AppRouter like this:
importtype{AppRouter}from`my-server`;
I want to be able to publish AppRouter types on NPM without exposing any sensitive information like my server source code or Context of the AppRouter.
Describe the solution you'd like to see
When generating .d.ts files via tsc or vite, .d.ts files contain all imported type definitions. Since AppRouter depends on Context, and Context usually contains all backend services (e.g., UsersService, PaymentService, etc), all the backend type declarations are also included in .d.ts files.
I see at least two solutions:
Somehow replace Context object with unknown when generating .d.ts files
If appRouter is defined in src/router.ts, only include dist/router.d.ts in package.json files field.
I hope trpc provides a recommended way to publish AppRouter, so developers do not need to think about it themselves.
Describe alternate solutions
Don't provide a recommended way of publishing AppRouter and leave it to devs.
Additional information
No response
👨👧👦 Contributing
🙋♂️ Yes, I'd be down to file a PR implementing this feature!
Reacted by hugofqt, Chiawen Chen, Aldo D'Aquino, Lars Berger, Christopher Yovanovitch, Patrick Ullrich and tkrizsa
Reacted by Lars Berger and Christopher Yovanovitch
According to the description of the linked PR, context object is still included in the router type(although context is no longer present in procedure types). Is it possible to remove context from .d.ts altogether?
The rest could technically be omitted with regards to the client.
However, in order to call router.createCaller() we'd need the Context.... potentially we could have a way to strip out stuff from routers that'd still work to create clients from.
Feel free to hack on this, know it's quite time consuming though, but you have pretty free reign to challenge how we do things seeing that it's a major change upcoming anyway
I still don't see a very easy way to export the types to a file and then import them somewhere else.
I ran into some circular dependencies and other errors when tryting to get tsc to emit a declaration file... Would love to see an npx command or similair to generate a file that ONLY has dependencies on @trpc/client and could be used anywhere (similar in nature to GraphQL Schema and utlimately the output you get from https://the-guild.dev/graphql/codegen)
Reacted by Aldo D'Aquino, Christopher Yovanovitch and Samuel Mortenson
Not sure it works with all configs, but it works at least with mine and may be useful to others. Here is the exposed router modifier I exports from the server:
importtype{AnyProcedure,AnyRootTypes,BuiltRouter,CreateRouterOptions,DecorateCreateRouterOptions,Simplify,}from'@trpc/server/unstable-core-do-not-import';typeExposedRootTypes<TextendsAnyRootTypes>=Simplify<{ctx: any;meta: any;errorShape: any;transformer: T['transformer'];}>;typeExposed<T>=Simplify<{[KinkeyofT]: T[K]extendsAnyProcedure
? T[K]// keeps the procedure as it is
: T[K]extendsBuiltRouter<any, infer TRouterDef>
? BuiltRouter<AnyRootTypes,Exposed<TRouterDef>>// remove root types
: T[K]extendsCreateRouterOptions
? DecorateCreateRouterOptions<Exposed<T[K]>>// deep exposition
: T[K];}>;/// From a TRPC router, deeply remove context and meta on routers / proceduresexporttypeExposedRouter<T>=TextendsBuiltRouter<
infer TRootTypes,
infer TProcedureRecord>
? BuiltRouter<ExposedRootTypes<TRootTypes>,Exposed<TProcedureRecord>>
: never;// usage exportPublicRouter=ExposedRouter<PrivateRouter>
Not sure it works with all configs, but it works at least with mine and may be useful to others. Here is the exposed router modifier I exports from the server:
import type {
AnyProcedure,
AnyRootTypes,
BuiltRouter,
CreateRouterOptions,
DecorateCreateRouterOptions,
Simplify,
} from '@trpc/server/unstable-core-do-not-import';
type Exposed = Simplify<{
[K in keyof T]: T[K] extends AnyProcedure
? T[K] // keeps the procedure as it is
: T[K] extends BuiltRouter<any, infer TRouterDef>
? BuiltRouter<AnyRootTypes, Exposed> // remove root types
: T[K] extends CreateRouterOptions
? DecorateCreateRouterOptions<Exposed<T[K]>> // deep exposition
: T[K];
}>;
/// From a TRPC router, deeply remove context and meta on routers / procedures
export type ExposedRouter = T extends BuiltRouter<
infer TRootTypes,
infer TProcedureRecord
? BuiltRouter<ExposedRootTypes, Exposed>
: never;
// usage
export PublicRouter = ExposedRouter
⚠ works with v11 only
how did you do that ? what is your private router ?
I try with the current source it gives never, and if I use the builtrouter on a .d.ts file in gives any :/
/**
* Initialization of tRPC backend
* Should be done only once per backend!
*/
const tNoCtx = initTRPC.create();
const t = initTRPC.context<Context>().create();
/**
* Export reusable router and procedure helpers
* that can be used throughout the router
*/
export const routerNoCtx = tNoCtx.router;
export const router = t.router;
export const publicProcedure = t.procedure;
index.tx:
const routes = {
/* in subroutes use pure object, nested router({...}) will contain ctx as well */
somepath: publicProcedure(...) ,
...
};
const appRouter = router(routes);
const appRouterNoCtx = routerNoCtx(routes);
export type AppRouter = typeof appRouterNoCtx;
export type AppRouterTypes = inferRouterOutputs<typeof appRouterNoCtx>;
Activity
KATT commentedon Nov 5, 2023
#4709 does this - implemented in next major (which you can use today)
olehmisar commentedon Nov 5, 2023
According to the description of the linked PR, context object is still included in the router type(although context is no longer present in procedure types). Is it possible to remove context from .d.ts altogether?
KATT commentedon Nov 5, 2023
Off the top of my head, the
@trpc/clientneedsThe rest could technically be omitted with regards to the client.
However, in order to call
router.createCaller()we'd need theContext.... potentially we could have a way to strip out stuff from routers that'd still work to create clients from.Feel free to hack on this, know it's quite time consuming though, but you have pretty free reign to challenge how we do things seeing that it's a major change upcoming anyway
matannahmani commentedon Aug 24, 2024
is this now available?
deldrid1 commentedon Sep 12, 2024
I still don't see a very easy way to export the types to a file and then import them somewhere else.
I ran into some circular dependencies and other errors when tryting to get
tscto emit a declaration file... Would love to see annpxcommand or similair to generate a file that ONLY has dependencies on@trpc/clientand could be used anywhere (similar in nature to GraphQL Schema and utlimately the output you get from https://the-guild.dev/graphql/codegen)yovanoc commentedon Mar 31, 2025
If someone already built something and can share it would be very nice!
hugo082 commentedon Apr 10, 2025
Not sure it works with all configs, but it works at least with mine and may be useful to others. Here is the exposed router modifier I exports from the server:
⚠ works with v11 only
yovanoc commentedon Apr 22, 2025
how did you do that ? what is your private router ?
I try with the current source it gives never, and if I use the builtrouter on a .d.ts file in gives any :/
tkrizsa commentedon Nov 29, 2025
My workaround:
trcp.ts:
index.tx:
KATT commentedon Nov 2, 2026
🤖 In v12 this is what contracts are for (experimental): a contract describes each procedure's input, output and errors in a package of its own, with no context or server code, and a client takes the contract as its
router. You can write the contract first and implement it witht.implement(), or derive one from an existing router withexperimental_toContract(router). https://v12.trpc.io/v12/docs/server/contract-first#clients-from-a-contract. v12 isn't released yet and there's no ETA.