Skip to content

Commit 349a6c5

Browse files
aaronshafclaude
andcommitted
fix(zod): add explicit type annotations to enum schemas for isolatedDeclarations
This change adds explicit z.ZodEnum type annotations to all generated enum schemas to make them compatible with TypeScript's isolatedDeclarations compiler option. - For regular enums: Added z.ZodEnum<typeof EnumType> annotation - For enumsAsTypes: Added z.ZodEnum<{ VALUE: "VALUE"; ... }> annotation Fixes #1187 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 30cfaa6 commit 349a6c5

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/zod/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,13 @@ export class ZodSchemaVisitor extends BaseSchemaVisitor {
192192
? new DeclarationBlock({})
193193
.export()
194194
.asKind('const')
195-
.withName(`${enumname}Schema`)
195+
.withName(`${enumname}Schema: z.ZodEnum<{ ${node.values?.map(enumOption => `${enumOption.name.value}: "${enumOption.name.value}"`).join('; ')} }>`)
196196
.withContent(`z.enum([${node.values?.map(enumOption => `'${enumOption.name.value}'`).join(', ')}])`)
197197
.string
198198
: new DeclarationBlock({})
199199
.export()
200200
.asKind('const')
201-
.withName(`${enumname}Schema`)
201+
.withName(`${enumname}Schema: z.ZodEnum<typeof ${enumTypeName}>`)
202202
.withContent(`z.nativeEnum(${enumTypeName})`)
203203
.string,
204204
);

tests/zod.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ describe('zod', () => {
227227
const result = await plugin(schema, [], { schema: 'zod', scalars }, {});
228228
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
229229
"
230-
export const PageTypeSchema = z.nativeEnum(PageType);
230+
export const PageTypeSchema: z.ZodEnum<typeof PageType> = z.nativeEnum(PageType);
231231
232232
export function PageInputSchema(): z.ZodObject<Properties<PageInput>> {
233233
return z.object({
@@ -252,7 +252,7 @@ describe('zod', () => {
252252
const result = await plugin(schema, [], { schema: 'zod', scalars, importFrom: './', schemaNamespacedImportName: 't' }, {});
253253
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
254254
"
255-
export const PageTypeSchema = z.nativeEnum(t.PageType);
255+
export const PageTypeSchema: z.ZodEnum<typeof t.PageType> = z.nativeEnum(t.PageType);
256256
257257
export function PageInputSchema(): z.ZodObject<Properties<t.PageInput>> {
258258
return z.object({
@@ -281,7 +281,7 @@ describe('zod', () => {
281281
const result = await plugin(schema, [], { schema: 'zod', scalars }, {});
282282
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
283283
"
284-
export const HttpMethodSchema = z.nativeEnum(HttpMethod);
284+
export const HttpMethodSchema: z.ZodEnum<typeof HttpMethod> = z.nativeEnum(HttpMethod);
285285
286286
export function HttpInputSchema(): z.ZodObject<Properties<HttpInput>> {
287287
return z.object({
@@ -443,7 +443,7 @@ describe('zod', () => {
443443
);
444444
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
445445
"
446-
export const PageTypeSchema = z.enum(['PUBLIC', 'BASIC_AUTH']);
446+
export const PageTypeSchema: z.ZodEnum<{ PUBLIC: "PUBLIC"; BASIC_AUTH: "BASIC_AUTH" }> = z.enum(['PUBLIC', 'BASIC_AUTH']);
447447
"
448448
`)
449449
});
@@ -468,7 +468,7 @@ describe('zod', () => {
468468
);
469469
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
470470
"
471-
export const PageTypeSchema = z.enum(['PUBLIC', 'BASIC_AUTH']);
471+
export const PageTypeSchema: z.ZodEnum<{ PUBLIC: "PUBLIC"; BASIC_AUTH: "BASIC_AUTH" }> = z.enum(['PUBLIC', 'BASIC_AUTH']);
472472
"
473473
`)
474474
});
@@ -711,7 +711,7 @@ describe('zod', () => {
711711
},
712712
);
713713

714-
expect(result.content).toContain('export const PageTypeSchema = z.nativeEnum(PageType)');
714+
expect(result.content).toContain('export const PageTypeSchema: z.ZodEnum<typeof PageType> = z.nativeEnum(PageType)');
715715
expect(result.content).toContain('export function PageInputSchema(): z.ZodObject<Properties<PageInput>>');
716716

717717
expect(result.content).toContain('pageType: PageTypeSchema.default(PageType.Public)');
@@ -747,7 +747,7 @@ describe('zod', () => {
747747
},
748748
);
749749

750-
expect(result.content).toContain('export const PageTypeSchema = z.nativeEnum(PageType)');
750+
expect(result.content).toContain('export const PageTypeSchema: z.ZodEnum<typeof PageType> = z.nativeEnum(PageType)');
751751
expect(result.content).toContain('export function PageInputSchema(): z.ZodObject<Properties<PageInput>>');
752752

753753
expect(result.content).toContain('pageType: PageTypeSchema.default(PageType.Basic_Auth)');
@@ -786,7 +786,7 @@ describe('zod', () => {
786786
},
787787
);
788788

789-
expect(result.content).toContain('export const PageTypeSchema = z.nativeEnum(PageType)');
789+
expect(result.content).toContain('export const PageTypeSchema: z.ZodEnum<typeof PageType> = z.nativeEnum(PageType)');
790790
expect(result.content).toContain('export function PageInputSchema(): z.ZodObject<Properties<PageInput>>');
791791

792792
expect(result.content).toContain('pageType: PageTypeSchema.default(PageType.BasicAuth)');
@@ -823,7 +823,7 @@ describe('zod', () => {
823823

824824
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
825825
"
826-
export const PageTypeSchema = z.nativeEnum(PageType);
826+
export const PageTypeSchema: z.ZodEnum<typeof PageType> = z.nativeEnum(PageType);
827827
828828
export function PageInputSchema(): z.ZodObject<Properties<PageInput>> {
829829
return z.object({
@@ -1369,9 +1369,9 @@ describe('zod', () => {
13691369

13701370
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
13711371
"
1372-
export const PageTypeSchema = z.nativeEnum(PageType);
1372+
export const PageTypeSchema: z.ZodEnum<typeof PageType> = z.nativeEnum(PageType);
13731373
1374-
export const MethodTypeSchema = z.nativeEnum(MethodType);
1374+
export const MethodTypeSchema: z.ZodEnum<typeof MethodType> = z.nativeEnum(MethodType);
13751375
13761376
export function AnyTypeSchema() {
13771377
return z.union([PageTypeSchema, MethodTypeSchema])
@@ -1787,7 +1787,7 @@ describe('zod', () => {
17871787
);
17881788
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
17891789
"
1790-
export const TestSchema = z.nativeEnum(Test);
1790+
export const TestSchema: z.ZodEnum<typeof Test> = z.nativeEnum(Test);
17911791
17921792
export function QueryInputSchema(): z.ZodObject<Properties<QueryInput>> {
17931793
return z.object({

0 commit comments

Comments
 (0)