Skip to content

Invalid types generated #1460

@borisovg

Description

@borisovg

The zod.ts file is generated with invalid types - we hit this when trying to use the package in our app but see (AI generated) repro (tested with graphql 6 and 7 packages) https://github.com/borisovg/zod-codegen-repro

pnpm repro
...
src/graphql.ts:65:3 - error TS2322: Type 'ZodObject<{ entered_options: ZodOptional<ZodNullable<ZodArray<ZodLazy<ZodNullable<ZodObject<Properties<EnteredOptionInput>, $strip>>>>>>; parent_sku: ZodOptional<...>; quantity: ZodNumber; selected_options: ZodOptional<...>; sku: ZodString; }, $strip>' is not assignable to type 'ZodObject<Properties<WishlistItemInput>, $strip>'.
  Type '{ entered_options: ZodOptional<ZodNullable<ZodArray<ZodLazy<ZodNullable<ZodObject<Properties<EnteredOptionInput>, $strip>>>>>>; parent_sku: ZodOptional<...>; quantity: ZodNumber; selected_options: ZodOptional<...>; sku: ZodString; }' is not assignable to type 'Properties<WishlistItemInput>'.
    The types of 'entered_options._input' are incompatible between these types.
      Type '({ uid: string | undefined; value: string | undefined; } | null)[] | null | undefined' is not assignable to type 'InputMaybe<InputMaybe<EnteredOptionInput>[]> | undefined'.
        Type '({ uid: string | undefined; value: string | undefined; } | null)[]' is not assignable to type 'InputMaybe<EnteredOptionInput>[]'.
          Type '{ uid: string | undefined; value: string | undefined; } | null' is not assignable to type 'InputMaybe<EnteredOptionInput>'.
            Type '{ uid: string | undefined; value: string | undefined; }' is not assignable to type 'InputMaybe<EnteredOptionInput>'.
              Types of property 'uid' are incompatible.
                Type 'string | undefined' is not assignable to type 'string'.
                  Type 'undefined' is not assignable to type 'string'.

65   return z.object({
     ~~~~~~

src/validation.ts:22:3 - error TS2322: Type 'ZodObject<{ entered_options: ZodOptional<ZodNullable<ZodArray<ZodLazy<ZodNullable<ZodObject<Properties<EnteredOptionInput>, $strip>>>>>>; parent_sku: ZodOptional<...>; quantity: ZodNumber; selected_options: ZodOptional<...>; sku: ZodString; }, $strip>' is not assignable to type 'ZodObject<Properties<WishlistItemInput>, $strip>'.
  Type '{ entered_options: ZodOptional<ZodNullable<ZodArray<ZodLazy<ZodNullable<ZodObject<Properties<EnteredOptionInput>, $strip>>>>>>; parent_sku: ZodOptional<...>; quantity: ZodNumber; selected_options: ZodOptional<...>; sku: ZodString; }' is not assignable to type 'Properties<WishlistItemInput>'.
    The types of 'entered_options._input' are incompatible between these types.
      Type '({ uid: string | undefined; value: string | undefined; } | null)[] | null | undefined' is not assignable to type 'InputMaybe<InputMaybe<EnteredOptionInput>[]>'.
        Type '({ uid: string | undefined; value: string | undefined; } | null)[]' is not assignable to type 'InputMaybe<InputMaybe<EnteredOptionInput>[]>'.
          Type '{ uid: string | undefined; value: string | undefined; } | null' is not assignable to type 'InputMaybe<EnteredOptionInput>'.
            Type '{ uid: string | undefined; value: string | undefined; }' is not assignable to type 'InputMaybe<EnteredOptionInput>'.
              Types of property 'uid' are incompatible.
                Type 'string | undefined' is not assignable to type 'string'.
                  Type 'undefined' is not assignable to type 'string'.

22   return z.object({
     ~~~~~~


Found 2 errors in 2 files.

Errors  Files
     1  src/graphql.ts:65
     1  src/validation.ts:22

AI suggestion for alternative types - I tested the v4 version and it seems to work but 🤷‍♂️

For Zod v4 output, generate:

type Properties<T> = {
  [K in keyof T]-?: z.ZodType<T[K], T[K]>;
};

Why this is better:

* -? forces the shape to include every key (as z.object({...}) expects).
* Optionality/nullability is still controlled by .optional()/.nullish() wrappers per field.
* Input/output stay aligned to the GraphQL type for that property (T[K]), instead of globally widening input to | undefined.

If they need compatibility with other Zod signatures, a robust internal alias could be:

type ZodSchemaFor<T> = z.ZodType<T, T>;
type Properties<T> = { [K in keyof T]-?: ZodSchemaFor<T[K]> };

(Or the v3-compatible 3-generic form if they support it in the same template.)

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