NotZelda
NotZelda4w ago

Is there a typescript performance gain if I infer my router outputs once, and then export them?

You can infer your router outputs using RouterOutputs["router"]["procedure"]. Should I just use this directly on the objects I want to pass this type through, or should I declare these once in a RouterOutputs file, and export a named type? Potentially if the traversal of the RouterOutputs type helper is expensive typescript could cache this result easier with a named export. Im just not sure if this is a reality or best practice?
1 Reply
NotZelda
NotZeldaOP4w ago
The example from the docs:
// @filename: client.ts
import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server';
import type { AppRouter } from './server';
 
type RouterInput = inferRouterInputs<AppRouter>;
type RouterOutput = inferRouterOutputs<AppRouter>;
 
type PostCreateInput = RouterInput['post']['create'];
//^ type PostCreateInput = {
// title: string;
// text: string;
// }
type PostCreateOutput = RouterOutput['post']['create'];

//^ type PostCreateOutput = AsyncIterable<Serialize<$T>, Serialize<$Return>, Serialize<$Next>>
// @filename: client.ts
import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server';
import type { AppRouter } from './server';
 
type RouterInput = inferRouterInputs<AppRouter>;
type RouterOutput = inferRouterOutputs<AppRouter>;
 
type PostCreateInput = RouterInput['post']['create'];
//^ type PostCreateInput = {
// title: string;
// text: string;
// }
type PostCreateOutput = RouterOutput['post']['create'];

//^ type PostCreateOutput = AsyncIterable<Serialize<$T>, Serialize<$Return>, Serialize<$Next>>

Did you find this page helpful?