Skip to content

zodv4 output produces unknown for optional nested input fields with exactOptionalPropertyTypes: true #1494

@borisovg

Description

@borisovg

(Note this is an AI-generated summary from troubleshooting session).

I’m seeing a TypeScript assignability error in generated zod.ts when using graphql-codegen-typescript-validation-schema with:

  • schema: 'zodv4'
  • imported generated types via importFrom
  • TypeScript exactOptionalPropertyTypes: true

In this setup, nested optional input fields can be emitted with unknown in Zod output typing, which then fails assignment to the generated GraphQL input types.

Possibly related to #1460 (similar need for manual type correction), but this report is specifically about zodv4 + exactOptionalPropertyTypes: true producing unknown in nested optional input output types.

Environment

  • graphql-codegen-typescript-validation-schema: 0.19.0
  • @graphql-codegen/cli: 7.1.2
  • @graphql-codegen/typescript: 6.0.2
  • TypeScript: strict mode with exactOptionalPropertyTypes: true
  • Zod: 4.x

Codegen config (relevant parts)

// codegen.ts
generates: {
  'src/magento/__generated__/types.ts': {
    plugins: ['typescript'],
    config: {
      inputMaybeValue: 'T | null | undefined',
      useTypeImports: true,
    },
  },
  'src/magento/__generated__/zod.ts': {
    plugins: ['typescript-validation-schema'],
    config: {
      schema: 'zodv4',
      importFrom: './types',
      schemaNamespacedImportName: 'types',
    },
  },
}

TS config (relevant parts)

{
  "compilerOptions": {
    "strict": true,
    "exactOptionalPropertyTypes": true
  }
}

Observed behaviour

Type '{ ... customizable_options?: unknown; ... } | null' is not assignable to type 'InputMaybe<BundleProductCartItemInput>'.
Types of property 'customizable_options' are incompatible.
Type 'unknown' is not assignable to type 'InputMaybe<InputMaybe<CustomizableOptionInput>[]>'.

Expected behaviour

Generated Zod schemas should typecheck with exactOptionalPropertyTypes: true, without degrading nested optional field types to unknown.

Additional notes

  • inputMaybeValue: 'T | null | undefined' is already set on TypeScript generation.
  • The issue appears specific to the zodv4 emitter path.
  • Behaviour is reproducible on nested optional input arrays/objects.

Workaround

Switch the validation schema emitter to zod while keeping Zod v4 runtime import:

'src/magento/__generated__/zod.ts': {
  plugins: ['typescript-validation-schema'],
  config: {
    schema: 'zod',
    zodImportPath: 'zod/v4',
    importFrom: './types',
    schemaNamespacedImportName: 'types',
  },
},

This avoids the unknown typing issue in our case while still using Zod v4.

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