Thanks to visit codestin.com
Credit goes to github.com

Skip to content

toZod (v3.0.0) is unable to infer union #20

@waful

Description

@waful
type ContrivedType = 1 | -1 | null;
const contrivedZod: toZod<ContrivedType> = z.union([z.literal(1), z.literal(-1), z.null()]);

ts complains:

Type 'ZodUnion<[ZodLiteral<1>, ZodLiteral<-1>, ZodNull]>' is not assignable to type 'ZodNullable<never> | ZodNullable<never>'.
  Property 'unwrap' is missing in type 'ZodUnion<[ZodLiteral<1>, ZodLiteral<-1>, ZodNull]>' but required in type 'ZodNullable<never>'.

If you remove toZod and do z.infer:

type ContrivedTypeInfer = z.infer<typeof contrivedZod>;

The inferred type is exactly type ContrivedTypeInfer = 1 | -1 | null

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