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

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?
3 Replies
Nick
Nick2y ago
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
haardik | LearnWeb3
It simply doesn't want to do that. It says comments is not a key in OuterType['innerKey']
Nick
Nick2y ago
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