feat(typescript-zod): emit literals for single-value enums by louis-bompart · Pull Request #3108 · glideapps/quicktype · GitHub
Skip to content

feat(typescript-zod): emit literals for single-value enums - #3108

Open
louis-bompart wants to merge 3 commits into
glideapps:masterfrom
coveord:agent/typescript-zod-const-enums
Open

feat(typescript-zod): emit literals for single-value enums#3108
louis-bompart wants to merge 3 commits into
glideapps:masterfrom
coveord:agent/typescript-zod-const-enums

Conversation

@louis-bompart

@louis-bompart louis-bompart commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Description

Emit z.literal(...) schemas for single-value TypeScript Zod string enums. Multi-value enums continue to use z.enum(...).

Related Issue

N/A

Motivation and Context

Single-value enums have a more precise and concise Zod representation. Per maintainer feedback, this behavior is always enabled rather than exposed as another renderer option.

Previous Behaviour / Output

export const KindSchema = z.enum([
    "only",
]);

New Behaviour / Output

export const KindSchema = z.literal("only");

How Has This Been Tested?

  • npm run build
  • npm run test:unit -- test/unit/typescript-zod-const-enum.test.ts
  • npx biome check packages/quicktype-core/src/language/TypeScriptZod/TypeScriptZodRenderer.ts packages/quicktype-core/src/language/TypeScriptZod/language.ts test/languages.ts test/unit/typescript-zod-const-enum.test.ts

The focused unit test verifies that a single-value enum emits a literal without any renderer option. The schema fixture continues to cover valid and invalid enum values through the normal TypeScript Zod fixture suite.

Screenshots (if appropriate)

N/A

@louis-bompart
louis-bompart marked this pull request as ready for review August 4, 2026 15:05
@schani

schani commented Aug 29, 2026

Copy link
Copy Markdown
Member

@github-actions

Copy link
Copy Markdown

Generated-output differences

37 files differ — 0 modified, 37 new, 0 deleted
2526 changed lines — +2526 / −0

Open the generated-output report →

@louis-bompart

Copy link
Copy Markdown
Contributor Author

Is there a good reason why this shouldn't just always be on? When would one genuinely want a one-element zod enum definition?

Mh, good question, I was honestly mimicking what's been done for the TypeScript language.
I was also trying not to introduce breaking change (not the best look to come around and propose breaking changes for my first contributions 😁)

@github-actions

Copy link
Copy Markdown

Generated-output differences

42 files differ — 0 modified, 42 new, 0 deleted
2843 changed lines — +2843 / −0

Open the generated-output report →

@schani

schani commented Aug 31, 2026

Copy link
Copy Markdown
Member

Is there a good reason why this shouldn't just always be on? When would one genuinely want a one-element zod enum definition?

Mh, good question, I was honestly mimicking what's been done for the TypeScript language. I was also trying not to introduce breaking change (not the best look to come around and propose breaking changes for my first contributions 😁)

Totally understandable :-). I don't think it's a breaking change, except in the sense that any change is breaking something, so if you wouldn't mind could you remove the option? I'm trying to minimize options.

@louis-bompart louis-bompart changed the title feat(typescript-zod): add prefer-const-values feat(typescript-zod): emit literals for single-value enums Aug 31, 2026
@louis-bompart

Copy link
Copy Markdown
Contributor Author

Done in bb7ff44: removed the prefer-const-values option and made z.literal(...) unconditional for single-value TypeScript Zod enums. Multi-value enums still use z.enum(...).

@github-actions

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants