Skip to content

feat(typescript-zod): preserve named object unions - #3103

Open
louis-bompart wants to merge 1 commit into
glideapps:agent/fix-issue-1646-2from
coveord:agent/zod-object-unions
Open

feat(typescript-zod): preserve named object unions#3103
louis-bompart wants to merge 1 commit into
glideapps:agent/fix-issue-1646-2from
coveord:agent/zod-object-unions

Conversation

@louis-bompart

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

Copy link
Copy Markdown

Description

Enables supportsUnionsWithMultipleObjectTypes for the TypeScript Zod target and extends the named-object-union regression test to cover Zod output.

Related Issue

Depends on #3061, which fixes #1646. This is intentionally a stacked draft PR.

Motivation and Context

#3061 adds guarded support for explicitly named, disjoint object alternatives. TypeScript Zod can represent those alternatives directly with z.union, so it can safely opt in without renderer changes.

Previous Behaviour / Output

Zod received the merged optional-properties object produced by the core union-flattening pass. Distinct alternatives such as Foo and Bar were not retained as separate schemas.

New Behaviour / Output

The generated Zod output preserves both schemas and references them from the containing schema:

export const FooSchema = z.object({ "foo": z.string() });
export const BarSchema = z.object({ "bar": z.string() });
export const ContainerSchema = z.object({
    "op": z.union([FooSchema, BarSchema]),
});

The Zod fixture also rejects an invalid union member.

How Has This Been Tested?

Tested locally with Node.js 24.18.0:

  • npm run build
  • npm run test:unit
  • QUICKTEST=true FIXTURE=schema-typescript-zod npm run test:fixtures -- test/inputs/schema/named-class-union.schema

The focused fixture round-trips both valid alternatives and verifies that the invalid sample fails.

Screenshots (if appropriate):

Not applicable.

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.

1 participant