Single element type of inferred router output array type

I'm trying to type a single object from an array but I don't want to manually create it. I've been using inferRouterOutputs so I was wondering if it's possible to remove the [] from the type so that it is just an object.
type RouterOutput = inferRouterOutputs<AppRouter>;
type AllItemsOutput = RouterOutput["items"]["getAllItems"];
type RouterOutput = inferRouterOutputs<AppRouter>;
type AllItemsOutput = RouterOutput["items"]["getAllItems"];
With AlItemsOutput being:
type AllItemsOutput = {
name: string | null;
id: string;
description: string | null;
}[] | undefined
type AllItemsOutput = {
name: string | null;
id: string;
description: string | null;
}[] | undefined
But I would like to convert it to:
type AllItemsOutput = {
name: string | null;
id: string;
description: string | null;
} | undefined
type AllItemsOutput = {
name: string | null;
id: string;
description: string | null;
} | undefined
5 Replies
erik.gh
erik.gh3y ago
What about something like this:
type ArrayItem<T extends any[]> = T extends (infer I)[] ? I : never;
type ArrayItem<T extends any[]> = T extends (infer I)[] ? I : never;
jix74
jix74OP3y ago
@erik.gh works! Thanks so much man!
erik.gh
erik.gh3y ago
great!
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
erik.gh
erik.gh3y ago
that's way cooler I did not know that

Did you find this page helpful?