T
tRPC

❓-help

How to infer type of a nested object from app router output?

Hhaardik5/9/2023
I have a tRPC router than returns a nested object through a db query. It looks like this:
ILessonCommentProps.comments: ({
_count: {
likes: number;
comments: number;
bookmarks: number;
};
comments: ({
user: GetResult<{
id: string;
displayName: string | null;
email: string | null;
emailVerified: Date | null;
... 17 more ...;
updatedAt: Date;
}, unknown>;
subComments: GetResult<...>[];
} & GetResult<...>)[];
bookmarks: {
...;
}[];
likes: {
...;
}[];
} & GetResult<...>) | null
ILessonCommentProps.comments: ({
_count: {
likes: number;
comments: number;
bookmarks: number;
};
comments: ({
user: GetResult<{
id: string;
displayName: string | null;
email: string | null;
emailVerified: Date | null;
... 17 more ...;
updatedAt: Date;
}, unknown>;
subComments: GetResult<...>[];
} & GetResult<...>)[];
bookmarks: {
...;
}[];
likes: {
...;
}[];
} & GetResult<...>) | null
I'd like to infer the type of the comments property to use as an interface for props on a component. I tried doing this but this doesn't work:
interface ILessonCommentProps {
comments: AppRouterOutputs['lessonBuilder']['getLessonLikesCommentsBookmarks']['comments'];
}
interface ILessonCommentProps {
comments: AppRouterOutputs['lessonBuilder']['getLessonLikesCommentsBookmarks']['comments'];
}
I was wondering if this is even possible?
Nnlucas5/9/2023
type InnerType = OuterType['innerKey'] and for an array element type InnerType = OuterType['innerKey'][number] So it looks like you are on the right track, more info on the error you've facing might be helpful
Hhaardik5/9/2023
It simply doesn't want to do that. It says comments is not a key in OuterType['innerKey']
Nnlucas5/9/2023
If you do keyof what keys are returned? You just need to investigate what the actual type is as you’re probably just navigating it wrong Unions and intersections can make this more complicated though, it does look like a complex type

Looking for more? Join the community!

T
tRPC

❓-help

Join Server
Recommended Posts
What's the type of errorFormatter parameterI wanna know how could i get the type of the errorFormatter parameter, so i can move the errorFormattRPC works only for monoliths environment?How can I works if typesafe in real time with a remote server? πŸ€”How to use createCaller() with lambda for testing?Has anyone successfully mocked a trpc caller that uses the AWS Lambda integration? trying to write sis there a way to call useQuery() from a callback and get the return value within that callback?I have a generic component that is effectively an autocomplete that fills in options from a web requUsing Response with the Next App RouterThe route handlers in the App Router, only receive the Request object, requiring you to use a ResponThe only way to access useQuery options without input is passing undefined to it?Is there any other way? seems strange pass undefined to the input queryHow can I access the trpc context in zod's refine function?To do something like ```ts create: myProcedure .input(myInputSchema.refine(async ({ slug }, ctx)Set Request Headers for individual requests?Hello everyone, I have a simple question, can you somehow add headers to the individual requests insGet undefined when the refetch function has successfully workedIn my use case, I first disabled the useQuery function because I want it only fetch when I want to. Having trouble to import my AppRouter in my react clientEnvironment: Node v18.15.0 I created my TRPC router in my express app and exported it how -documeArgument of Type not assignableSeeing the following error while trying to establish a connection with the trpc server from the clieCan't I create multiple TRPC React using same Router?Im using monorepo and have 2 client projects on that. I have 2 files that does the same, create the what is architectural style of nextjs + prisma + trpc app?like REST.... or SOA?Types with enabled: falseHow do types work with enabled: false? Do I need to cast my arguments in the cases where enabled is Error: No QueryClient set, use QueryClientProvider to set oneHello! I have wrapped my _app.tsx properly and I have made useQuery functionality possible, howeverYou're trying to use @trpc/server in a non-server environmentEnvironment: Node v18.15.0, yarn, Next 13.2.4 What's wrong: When using `createServerSideHelpers`, i