Skip to content

z.union type inside z.object returns (UnionType) & (UnionType | undefined) when z.array() is one of the options #2325

@anthonyma94

Description

@anthonyma94

When an array is one of the options in z.union inside z.object, the inferred type becomes (UnionType) & (UnionType | undefined) instead of just UnionType.

const UnionType = z.object({
  test: z.union([z.string(), z.number()])
});
type UnionType = z.infer<typeof UnionType> // { test: string | number }

const UnionTypeBug = z.object({
  test: z.union([z.string(), z.number().array()])
});
type UnionTypeBug = z.infer<typeof UnionTyptBug> // { test: (string | number[]) & (string | number[] | undefined) }, should be { test: string | number[] }

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