Skip to content

Generated enum schemas lack explicit types, causing conflict with isolatedDeclarations #1187

@aaronshaf

Description

@aaronshaf

Issue Description

When using graphql-codegen-typescript-validation-schema in a TypeScript project with the isolatedDeclarations: true compiler option, the build fails. The root cause is that the Zod schemas generated for GraphQL enums do not have explicit type annotations, which is a requirement under isolatedDeclarations.

This forces us to either disable a valuable TypeScript compiler feature or avoid using the plugin for enum validation.

Environment

  • graphql-codegen: 5.0.7
  • graphql-codegen-typescript-validation-schema: 0.17.1
  • typescript: 5.6.2
  • zod: 3.25.67
  • tsconfig.json with "isolatedDeclarations": true

Steps to Reproduce

  1. Define a GraphQL schema with an enum:
enum Status {
  DRAFT
  PUBLISHED
  ARCHIVED
}

input PostInput {
  status: Status\!
}
  1. Configure codegen.ts to use the typescript-validation-schema plugin:
import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  schema: 'schema.graphql',
  generates: {
    './src/validation.ts': {
      plugins: [
        {
          'typescript-validation-schema': {
            schema: 'zod',
            // ... other options
          },
        },
      ],
    },
  },
};

export default config;
  1. Run the code generator.

Problematic Output

The plugin generates the following code for the enum, which lacks a type annotation:

// validation.ts
import { z } from 'zod';

export const StatusSchema = z.enum(['DRAFT', 'PUBLISHED', 'ARCHIVED']);

Expected Output

For compatibility with isolatedDeclarations, the generated code should include an explicit type annotation:

// validation.ts
import { z } from 'zod';

export const StatusSchema: z.ZodEnum<["DRAFT", "PUBLISHED", "ARCHIVED"]> = z.enum(['DRAFT', 'PUBLISHED', 'ARCHIVED']);

Compiler Error

When another file imports StatusSchema, the TypeScript compiler throws an error:

error TS9023: Variable 'StatusSchema' must have an explicit type annotation with --isolatedDeclarations.

Suggested Solution

The plugin should be updated to add explicit z.ZodEnum<...> type annotations to all exported enum schemas it generates. This would make the output compatible with modern TypeScript project configurations that enable isolatedDeclarations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions