Below is the example code of my issue. I'm using tRPC and Zod in the example.
import { initTRPC, inferRouterOutputs } from '@trpc/server';
import { z } from "zod";
const t = initTRPC.create();
const { router, procedure } = t;
interface ITest {
[key: string]: object | unknown;
prop: string;
}
export const appRouter = router({
foo: procedure.input(z.undefined()).query(() => {
let test: ITest = { prop: "test" };
return test;
}),
});
export type AppRouter = typeof appRouter;
type bar = inferRouterOutputs<AppRouter>["foo"];
// ^?
// type bar = { [x: string]: never; [x: number]: never; }
// expected: ITest
Here, bar is typed as
type bar = {
[x: string]: never;
[x: number]: never;
}
This results in a type mismatch, as the query data is no longer typed as ITest
nor has the prop
property, contrary to the actual data. Is this default TypeScript behavior or tRPC? How do I work around this problem?