这是indexloc提供的服务,不要输入任何密码
Skip to content

The recursive reference: issue type with get TS2322 #5442

@ArnaudPanaiotisSeira

Description

@ArnaudPanaiotisSeira

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, children

398 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions