-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Hello,
I'm trying to rework code on a project using get to define recursivity.
simplified code exemple
I have this zod zChat and the inferring type Chat linked: (this work fine)
export const zChat = z.object({
name: z.string(),
get children(): z.ZodArray<typeof zChat> {
return z.array(zChat);
},
});
export type Chat = z.infer<typeof zChat>;
I'm forced to type get children() as it's part of the project.
I try to use it in my code that leads to errors, code:
const mamanChat: Chat = zChat.parse({
name: "Swan",
children: [
{ name: "filou", children: [] },
{ name: "jambon", children: [] }
]
})
let chat: Chat[] = mamanChat.children;
Type 'Record<string, unknown>[]' is not assignable to type '{ name: string; children: Record<string, unknown>[]; }[]'.
Type 'Record<string, unknown>' is missing the following properties from type '{ name: string; children: Record<string, unknown>[]; }': name, children398 let chat: Chat[] = mamanChat.children;
Any idea how to makes children to be a Chat[] within infer instead of a Record<string, unknown>[] ?
I don't want to parse/cast every time I access my childrens.